# OpenClaw Cron 定时任务权限问题排查与解决
**日期**: 2026-04-08
**状态**: ✅ 已解决
**标签**: #openclaw #cron #定时任务 #权限 #配对 #排错
---
## 问题描述
在 OpenClaw 中创建定时任务(Cron Job)时,无论通过网页 UI 还是命令行 CLI,都会报错失败:
```
gateway connect failed: GatewayClientRequestError: pairing required
[tools] cron failed: gateway closed (1008): pairing required
```
同时 gateway 日志显示:
```
security audit: device access upgrade requested reason=scope-upgrade
scopesFrom=operator.read scopesTo=operator.admin
```
即:创建 cron 任务需要 `operator.admin` 权限,但当前设备只有 `operator.read`。
---
## 排查过程
### 第一阶段:确认错误类型
最初以为是技能文件里让 AI 模型自行调用 `cron.add` 导致的崩溃。通过分析日志发现错误链路:
1. 模型执行技能 → 调用 `cron.add` 工具
2. `cron.add` 需要 `operator.admin` 权限
3. 当前设备只有 `operator.read` → 触发 **scope-upgrade**(权限升级请求)
4. 权限升级需要设备配对审批(pairing)→ 本地连接未完成配对
5. 连接被拒绝(1008: pairing required) → 操作失败
### 第二阶段:定位权限根因
使用以下命令查看设备状态:
```bash
openclaw devices list
```
输出结果:
| 设备ID | 权限 | 状态 |
| `<device-id-1>` | operator.admin, write, read, ... | Paired ✅ |
| **`<device-id-本机>`**(本机CLI/UI) | **operator.read** ❌ | Paired(权限不足) |
| `<device-id-3>` | operator.admin, write, read, ... | Paired ✅ |
发现:
- 本机设备的权限只有 `operator.read`
- 存在一个 Pending(待审批)的权限升级请求
- 该请求正是之前尝试创建 cron 时自动触发的
### 第三阶段:批准配对请求
```bash
openclaw devices approve <request-id>
```
输出:**Device pairing request approved**
批准后,本机设备获得了完整的 admin 权限。
### 第四阶段:重新创建定时任务
```bash
openclaw cron add \
--name "政策会议追踪" \
--description "每周一自动检查当前时间节点是否有政策会议,抓取报告并分析" \
--cron "0 9 * * 1" \
--tz "Asia/Shanghai" \
--session isolated \
--message "执行政策会议追踪技能:检查当前时间节点是否有关键政策会议..." \
--thinking low \
--announce \
--timeout-seconds 120
```
输出:
```
✅ Cron job created successfully
ID: <job-id>
Name: 政策会议追踪
Status: enabled
Next run: <next-run-time>
```
**创建成功!✅**
---
## 根因总结
| 项目 | 说明 |
| **根本原因** | 设备初次配对时只授予了 `operator.read` 权限,而 `cron.add` / `config.patch` 等管理操作需要 `operator.admin` |
| **触发条件** | 任何需要 admin 权限的操作(创建定时任务、修改配置等) |
| **表现症状** | `pairing required` / `scope-upgrade` / gateway 连接断开 |
| **解决方法** | 批准待审批的配对请求,授予 admin 权限 |
---
## 关键命令速查
### 设备管理
```bash
# 查看所有已配对设备和待审批请求
openclaw devices list
# 批准配对请求(获取 request-id 从 devices list 的 Pending 区域)
openclaw devices approve <request-id>
# 拒绝配对请求
openclaw devices reject <request-id>
```
### Cron 定时任务管理
```bash
# 创建定时任务
openclaw cron add --name "名称" --cron "0 9 * * 1" --tz "Asia/Shanghai" --session isolated --message "任务描述" --announce
# 列出所有定时任务
openclaw cron list
# 启用/禁用任务
openclaw cron enable <job-id>
openclaw cron disable <job-id>
# 手动立即执行一次
openclaw cron run <job-id>
# 删除任务
openclaw cron rm <job-id>
```
### Cron 表达式参考
| 表达式 | 含义 |
| `0 9 * * 1` | 每周一早上 9:00 |
| `0 9 * * *` | 每天早上 9:00 |
| `0 */2 * * *` | 每 2 小时 |
| `30 8 * * 5` | 每周五早上 8:30 |
| `0 9 1 * *` | 每月 1 号早上 9:00 |
---
## 经验教训
1. **首次配对不等于完全授权**:OpenClaw 的设备配对是分级的,初始可能只给 read 权限,admin 权限需要单独审批
2. **遇到 pairing required 先查设备**:不要盲目重试,先用 `openclaw devices list` 看 Pending 请求
3. **命令行比 UI 更适合排错**:网页 UI 的错误信息不够详细,CLI 日志能看到完整的 security audit 链路
4. **Cron 创建和执行应该分离**:定时任务预先配好,技能/模型只需要关注执行逻辑,不需要自己创建 cron
---
## 相关文件
- OpenClaw 配置: `~/.openclaw/openclaw.json`
- Gateway 日志: `/tmp/openclaw/openclaw-YYYY-MM-DD.log`