Skip to content

openclaw cron 是 OpenClaw Gateway 的定时任务管理命令。支持周期性任务(cron 表达式)和一次性定时触发(--at),可将任务结果通过 Telegram、Slack 等频道投递,也可以静默在后台执行。支持独立 session 隔离、指数退避重试和多账号多频道投递,是让龙虾自动化工作的核心工具。

openclaw cron

管理 Gateway 调度器的定时任务(cron jobs)。

相关文档:

运行 openclaw cron --help 查看完整命令列表。

重要说明

  • 独立的 cron add 任务默认启用 --announce 投递。使用 --no-deliver 可将输出保留在内部不发送。--deliver 作为 --announce 的废弃别名仍然支持。
  • 独立 cron 运行拥有最终发送路径。--no-deliver 保持运行内部;不会将投递交还给 agent 的消息工具。
  • 一次性任务(--at)成功执行后默认自动删除。使用 --keep-after-run 可保留。
  • --session 支持 mainisolatedcurrentsession:<id>。使用 current 绑定到创建时的活跃会话,或用 session:<id> 指定持久会话键。
  • 对于一次性 CLI 任务,没有时区偏移的 --at 时间视为 UTC,除非同时传入 --tz <iana> 将其解释为指定时区的本地挂钟时间。
  • 周期性任务在连续出错后会使用指数退避重试(30s → 1m → 5m → 15m → 60m),下次成功运行后恢复正常调度。
  • openclaw cron run 现在在手动运行入队后立即返回。成功响应包含 { ok: true, enqueued: true, runId },使用 openclaw cron runs --id <job-id> 跟踪最终结果。
  • openclaw cron run <job-id> 默认强制运行。使用 --due 保留旧的"仅在到期时运行"行为。
  • 独立 cron 任务会抑制过时的纯确认回复。如果第一个结果只是临时状态更新而没有子 agent 负责最终答案,cron 会重新提示一次以获取真实结果再投递。
  • 如果独立 cron 运行只返回静默 token(NO_REPLY / no_reply),cron 同时抑制直接出站投递和回退的排队摘要路径,不向聊天发送任何内容。
  • cron add|edit --model ... 使用该选定的允许模型执行任务。如果模型不被允许,cron 会发出警告并回退到任务的 agent/默认模型选择。
  • 日志保留/清理由配置控制:
    • cron.sessionRetention(默认 24h):清理已完成的独立运行 session。
    • cron.runLog.maxBytes + cron.runLog.keepLines:清理 ~/.openclaw/cron/runs/<jobId>.jsonl

升级提示:如果你有旧版 cron 任务(早于当前投递/存储格式),请运行 openclaw doctor --fix。Doctor 会规范化遗留 cron 字段,并将简单的 notify: true webhook 回退任务迁移到显式 webhook 投递(当 cron.webhook 已配置时)。

常见编辑操作

更新投递设置而不修改消息内容:

bash
openclaw cron edit <job-id> --announce --channel telegram --to "123456789"

禁用独立任务的投递:

bash
openclaw cron edit <job-id> --no-deliver

为独立任务启用轻量引导上下文:

bash
openclaw cron edit <job-id> --light-context

向指定频道发布通知:

bash
openclaw cron edit <job-id> --announce --channel slack --to "channel:C1234567890"

创建带轻量引导上下文的独立任务,让你的龙虾每天早上自动汇报:

bash
openclaw cron add \
  --name "Lightweight morning brief" \
  --cron "0 7 * * *" \
  --session isolated \
  --message "Summarize overnight updates." \
  --light-context \
  --no-deliver

--light-context 仅适用于独立 agent-turn 任务。在轻量模式下,cron 运行时的引导上下文会保持为空,而不是注入完整的工作区引导集。

投递归属说明:

  • Cron 拥有的独立任务始终通过 cron runner 路由最终用户可见的投递(announcewebhook 或仅内部的 none)。
  • 如果任务提到向某个外部接收方发送消息,agent 应在结果中描述预期目标,而不是尝试直接发送。

常见管理命令

手动运行:

bash
openclaw cron run <job-id>
openclaw cron run <job-id> --due
openclaw cron runs --id <job-id> --limit 50

Agent/会话重定向:

bash
openclaw cron edit <job-id> --agent ops
openclaw cron edit <job-id> --clear-agent
openclaw cron edit <job-id> --session current
openclaw cron edit <job-id> --session "session:daily-brief"

投递调整:

bash
openclaw cron edit <job-id> --announce --channel slack --to "channel:C1234567890"
openclaw cron edit <job-id> --best-effort-deliver
openclaw cron edit <job-id> --no-best-effort-deliver
openclaw cron edit <job-id> --no-deliver

失败投递说明:

  • delivery.failureDestination 支持独立任务。
  • 主会话任务仅在主投递模式为 webhook 时才可使用 delivery.failureDestination
  • 如果你没有设置任何失败目标,且任务已向频道发布通知,失败通知会复用同一发布目标。

常见问题

Q: 定时任务到点了但没有触发,怎么排查?

A: 先运行 openclaw gateway status 确认 Gateway 在线,再用 openclaw cron runs --id <job-id> 查看运行日志。如果日志为空,检查 cron 表达式是否有误(可用 crontab.guru 验证),以及 Gateway 是否在任务触发时段内持续运行。

Q: --no-deliver--announce 有什么区别?

A: --announce 将任务结果投递到指定频道(如 Telegram/Slack),--no-deliver 则只在内部执行不发送任何消息。独立任务默认 --announce,如果你只想让龙虾静默执行某些后台任务,加上 --no-deliver 即可。

Q: 如何让某个 cron 任务只运行一次?

A: 在 cron add 时使用 --at 指定一个未来时间,例如 --at "2026-05-01T09:00:00",任务成功运行后会自动删除。若希望保留任务记录,加上 --keep-after-run