背景 链接到标题

OpenClaw Gateway 内置了一个调度器(Cron),用于定时执行后台任务。它的执行模型分为两种:

  1. Command cron——不调用模型,直接在 Gateway 进程内执行 shell 命令
  2. Agent turn cron——启动 isolated agent,走完整的 LLM 对话流程

这两种模型在定位、适用场景和失败边界上有本质区别。本文以 Wiki 编译这个实际案例切入,说明如何选择。

Command Cron 链接到标题

是什么 链接到标题

Command cron 是 openclaw cron add/create 的一个执行模式。它不启动 agent,不调用模型,直接在 Gateway 进程内执行 shell 命令。

官方 [openclaw cron --help] 的描述:

--command <shell> — Command payload run as sh -lc on the Gateway

底层等价于定时运行 sh -lc "<command>",捕获 stdout/stderr,记录到 cron run history。

适用场景 链接到标题

  • Wiki 编译:openclaw wiki bridge import --json && openclaw wiki compile --json && openclaw wiki lint --json
  • 系统巡检、磁盘检查、日志轮转
  • 定时备份、同步脚本
  • 队列深度探测

这些任务的共同特征:执行逻辑完全由工程代码定义,不需要 AI 理解、判断或生成

行为特征 链接到标题

项目 说明
执行体 sh -lc <command>--command-argv 精确 argv
结果判定 退出码 0 → ok;非零/超时/信号 → error
输出 stdout/stderr,通过 announce/webhook 投递
超时 --timeout-seconds(整体)、--no-output-timeout-seconds(无输出时)
权限 需要 operator.admin 才能创建/编辑/手动运行
投递 复用 announcewebhooknone 三种模式

最佳实践 链接到标题

openclaw cron create "0 5 * * *" \
  --name "daily-wiki-compile" \
  --announce \
  --channel last \
  --command "openclaw wiki bridge import --json && openclaw wiki compile --json && openclaw wiki lint --json"

Command 输出中可以 print NO_REPLY 抑制投递,适合只记录不通知的维护任务。

Agent Turn Cron 链接到标题

是什么 链接到标题

Agent turn cron 是传统的 cron 方式:定时创建一个 isolated agent session,发送 prompt,等 LLM 回复。

官方文档的描述:

Positional arguments: [scheduleOrName] [message]
--message <text> — Agent message payload

它走的是完整的 agent 运行路径:

scheduler → isolated agent → prompt assembly → model stream → tool calls → reply

适用场景 链接到标题

  • 每日摘要"Summarize overnight updates"
  • 定时简报"Summarize project updates from Slack"
  • 邮件检查与处理"📧 邮件检查时间 - 请按照工作流程检查邮箱"
  • 数据趋势分析"Analyze today's metrics and highlight anomalies"
  • 复杂编排:需要 Agent 判断、规划、分步执行的任务

行为特征 链接到标题

项目 说明
执行体 LLM Agent(可指定 model/thinking/tools)
结果判定 Agent 回复 + tool call 结果
输出 模型回复文本
超时 --timeout-seconds 整体超时
模型 --model 覆盖(支持 fallback 链)
成本 每次执行产生 token 消耗

创建示例 链接到标题

openclaw cron create "0 7 * * *" \
  "Summarize overnight updates." \
  --name "Morning brief" \
  --model deepseek/deepseek-v4-flash \
  --thinking off \
  --announce \
  --channel slack

区别对比 链接到标题

维度 Command cron Agent turn cron
执行体 shell 命令/CLI OpenClaw Agent + LLM
模型调用 不调用 调用配置的模型
执行路径 scheduler → runner → process scheduler → isolated agent → prompt → stream → tools
结果判定 退出码 + stdout/stderr LLM 回复文本
失败面 命令超时、非零退出、信号 模型超时、stream 中断、tool call 错误、provider 错误
可复现性 高——同一命令总是产生相同行为 中——模型输出有随机性
Token 消耗 每次运行消耗 token
超时控制 --timeout-seconds + --no-output-timeout-seconds --timeout-seconds
创建方式 --command <shell> 位置参数 prompt / --message <text>
权限 需要 operator.admin 需要 operator.admin

如何选择 链接到标题

基本原则 链接到标题

确定性任务用 command,非确定性任务用 agentTurn。

涉及以下特征的,优先用 command:

  • 输入和输出可以精确描述
  • 成功/失败边界清晰
  • 执行过程不需要 AI 理解
  • 希望结果严格可复现

涉及以下特征的,考虑用 agentTurn:

  • 输入是自然语言描述的需求
  • 输出需要 AI 生成或判断
  • 执行过程需要多步工具编排
  • 需要模型理解上下文

错误案例 链接到标题

旧方式的 Wiki 编译使用了 agentTurn:

payload.kind = agentTurn
message = /wiki-compile

实际做的事情是:让 LLM 理解 /wiki-compile 指令,再调用 wiki 工具链执行编译。Wiki 编译本身是确定性工程动作(bridge import → compile → lint),让 LLM 参与调度引入了不必要的失败面。

正确做法 链接到标题

Wiki 编译改为 command cron:

--command "openclaw wiki bridge import --json && openclaw wiki compile --json && openclaw wiki lint --json"

而每日摘要用 agentTurn:

openclaw cron create "0 8 * * *" \
  "Summarize yesterday's key events." \
  --name "Daily brief"

经验总结 链接到标题

  1. 模式选择比模型选择更影响稳定性——agentTurn 在模型 provider 异常或多轮 tool call 失败时容易中断,command cron 的失败面只有命令退出码和超时
  2. Command cron 是 operator admin 权限——创建、编辑、手动运行都需要 operator.admin,不能在 agent 对话中由 LLM 创建
  3. 升级验证后保留 command cron——即使修复了 agentTurn 在特定模型下的 bug,确定性任务仍然推荐 command cron
  4. 两者可以混合使用——凌晨用 command cron 做编译维护,白天用 agentTurn cron 做信息摘要,互不冲突