OpenClaw 自动化编排:Cron 精准调度与 Heartbeat 批处理
在7×24小时运行的AI Agent生产环境中,"定时触发"与"健康巡检"是自动化运维的双支柱。OpenClaw通过六字段Cron引擎(支持秒级精度)与可编排的Heartbeat机制,实现了从定时任务到自愈式检查清单的完整自动化闭环。本文深度拆解其调度语义、执行隔离策略与投递模式选型。
一、自动化编排架构概览
OpenClaw的自动化体系由Cron任务调度器与Heartbeat健康检查器两大组件构成,分别应对"计划性任务"与"周期性巡检"两类场景。
md
+-------------------+ +-------------------+ +-------------------+
| 时间触发源 | | 调度决策引擎 | | 执行运行时 |
| (Cron/Heartbeat) |------->| (Priority Queue) |------->| (Session Router) |
+-------------------+ +-------------------+ +-------------------+
| | |
v v v
+--------+----------+ +---------+---------+ +---------+---------+
| Cron Expressions | | 投递模式选择 | | 会话隔离策略 |
| - 6-field (sec) | | - announce | | - Main Session |
| - at (once) | | - webhook | | - Isolated Session|
| - every (interval)| | - none | | - cron:<jobId> |
| - cron (standard) | | | | |
+-------------------+ +-------------------+ +-------------------+
二、Cron任务调度:六字段精准定时
OpenClaw的Cron引擎支持标准六字段表达式(含秒级),相比传统Linux Cron的五字段格式,提供了更高精度的事件触发能力。
2.1 六字段表达式规范
| 字段 | 范围 | 说明 | 示例 |
|---|---|---|---|
| 秒 | 0-59 | 可选,默认0 | 0/30 每30秒 |
| 分 | 0-59 | 必填 | */5 每5分钟 |
| 时 | 0-23 | 必填 | 9-18 工作时间 |
| 日 | 1-31 | 必填 | L 月末最后一日 |
| 月 | 1-12 | 必填 | 1,4,7,10 季度首月 |
| 周 | 0-7 | 必填(0/7=周日) | 1-5 工作日 |
表达式示例:
bash
# 每30秒执行(高频监控)
*/30 * * * * *
# 工作日每15分钟执行(业务检查)
0 */15 * * * 1-5
# 每月1日及15日上午9点执行(报表生成)
0 0 9 1,15 * *
# 月末最后一天23:59执行(月度归档)
59 59 23 L * *
2.2 三种时间语义模式
OpenClaw Cron支持三种时间触发语义,通过CLI openclaw cron add 的 --type 参数指定:
- at - 一次性定时(One-shot)
bash
# 明天下午3点执行一次
openclaw cron add "at:2026-03-12 15:00" --agent main --prompt "检查服务器磁盘"
执行特征:
- 精确到分钟级触发
- 执行后自动从调度表移除
- 适用于延迟任务、提醒通知
- every - 固定间隔(Fixed Interval)
bash
# 每2小时执行一次
openclaw cron add "every:2h" --agent maintenance --prompt "巡检日志"
# 每30秒执行(高频采集)
openclaw cron add "every:30s" --agent monitor --prompt "采集指标"
执行特征:
- 基于上次执行完成时间计算下次触发点
- 支持单位:s(秒)、m(分)、h(时)、d(天)
- 适用于周期性维护、数据采集
- cron - 标准表达式(Calendar Scheduling)
bash
# 标准六字段(含秒)
openclaw cron add "cron:0 0 9 * * 1-5" --agent work --prompt "晨会准备"
# 五字段简写(秒默认为0)
openclaw cron add "cron:0 9 * * 1-5" --agent work --prompt "晨会准备"
执行特征:
- 基于日历时间触发,与执行时长无关
- 支持Cron特殊字符:* , - / L W #
- 适用于定时报表、定时同步
2.3 CLI全生命周期管理
bash
# 添加任务(交互式)
openclaw cron add
# 提示输入:时间表达式、Agent、提示词、投递模式
# 添加任务(非交互式)
openclaw cron add "cron:0 0 * * * *" \
--agent main \
--prompt "每日健康检查" \
--mode announce \
--label "daily-health"
# 列出所有任务(含下次触发时间)
openclaw cron list
# 输出示例:
# ID | Schedule | Agent | Next Run | Status
# ------+------------------+-------+--------------------+--------
# cron-1| 0 0 * * * * | main | 2026-03-10 00:00:00| active
# cron-2| every:30m | log | 2026-03-09 14:30:00| active
# 查看执行历史
openclaw cron runs --id cron-1 --limit 10
# 暂停/恢复任务
openclaw cron pause cron-1
openclaw cron resume cron-1
# 删除任务
openclaw cron remove cron-1
三、Heartbeat机制:批处理健康巡检
与Cron的"固定时间触发"不同,Heartbeat是"固定间隔批量检查"机制,专为Main Session的健康状态巡检设计。
3.1 Heartbeat核心逻辑
md
+---------------+ +------------------+ +-------------------+
| 定时触发器 | | HEARTBEAT.md | | 检查项批量执行 |
| (默认30分钟) |---->| 检查清单解析 |---->| (Agent循环) |
+---------------+ +------------------+ +-------------------+
^ | |
| v v
| +--------+--------+ +--------+--------+
| | 多源检查项聚合 | | 结果聚合与投递 |
| | - Main Session | | - announce |
| | - Memory Search | | - webhook |
| | - External API | | - none |
| +-------------------+ +------------------+
|
+-------------------------------------------------------------+
(循环触发)
默认配置(openclaw.json):
json
{
heartbeat: {
every: "30m", // 触发间隔
mode: "announce", // 投递模式
target: "main", // 目标Agent(默认Main Session)
// 检查清单文件路径(默认 workspace/HEARTBEAT.md)
checklist: "~/.openclaw/workspace/HEARTBEAT.md"
}
}
3.2 HEARTBEAT.md检查清单规范
Heartbeat的核心是可编排的检查清单文件,采用Markdown格式定义要执行的健康检查项:
md
# HEARTBEAT.md - 系统健康检查清单
## 检查项1:待办事项状态
检查当前所有待办事项(TODOs),识别逾期任务,生成优先级排序的摘要报告。
## 检查项2:会话健康度
分析最近24小时的会话历史,识别异常中断、错误率过高的会话,提出优化建议。
## 检查项3:外部服务连通性
检查配置的外部API(如数据库、第三方服务)连通状态,记录响应延迟与错误率。
## 检查项4:磁盘与资源
检查工作区磁盘使用率,若超过80%触发清理提醒;检查内存占用趋势。
## 检查项5:安全审计
审查最近的文件访问日志,识别异常操作模式;检查敏感凭证过期时间。
执行特征:
- 每个检查项作为独立Prompt提交给Agent
- Agent可调用工具(如memory_search、fs_read、exec)获取数据
- 结果按mode配置投递,默认生成结构化摘要
3.3 手动触发与调试
bash
# 立即触发一次Heartbeat(调试)
openclaw heartbeat trigger
# 指定Agent触发
openclaw heartbeat trigger --agent maintenance
# 查看Heartbeat执行历史
openclaw heartbeat runs --limit 5
四、执行模式:Main Session vs Isolated Session
自动化任务面临上下文隔离性与状态连续性的权衡。OpenClaw提供两种执行模式,通过--session参数控制。
4.1 执行模式对比
| 维度 | Main Session (System Event) | Isolated Session (cron:) |
|---|---|---|
| 会话Key | agent:<id>:main |
cron:<jobId> |
| 上下文继承 | 完整继承Main Session历史 | 独立上下文,无历史负担 |
| 并发影响 | 与用户对话串行(Lane排队) | 独立Lane,并行执行 |
| 状态副作用 | 影响Main Session记忆 | 隔离,仅输出结果 |
| 适用场景 | 用户感知的自动化(如每日摘要) | 后台任务(如日志归档) |
4.2 模式选择决策树
md
+-------------------+
| 任务类型判断 |
+---------+---------+
|
+----+----+---------+
| | |
v v v
+----+---+ +---+---+ +---+----+
| 用户可见?| |后台处理?| |定时同步?|
| 每日简报 | | 数据清理 | | API采集 |
| 提醒通知 | | 日志归档 | | 报表生成 |
+----+----+ +----+----+ +----+----+
| | |
v v v
+----+----+ +---+----+ +---+----+
|Main | |Isolated| |Isolated|
|Session | |Session | |Session |
|(System | |(cron:*) | |(cron:*) |
| Event) | | | | |
+---------+ +--------+ +--------+
4.3 配置实例
Main Session模式(用户感知):
bash
# 每日9点生成摘要,投递到Main Session
openclaw cron add "cron:0 0 9 * * *" \
--agent main \
--prompt "生成昨日工作总结" \
--mode announce \
--session main
Isolated Session模式(后台任务):
bash
# 每小时清理临时文件,不影响用户对话
openclaw cron add "cron:0 0 * * * *" \
--agent maintenance \
--prompt "清理workspace/tmp目录下超过7天的文件" \
--mode none \
--session isolated
配置等价写法(JSON配置):
json5
{
cron: {
jobs: [
{
id: "daily-summary",
schedule: "0 0 9 * * *",
agentId: "main",
prompt: "生成昨日工作总结",
mode: "announce",
session: "main" // System Event
},
{
id: "cleanup-tmp",
schedule: "0 0 * * * *",
agentId: "maintenance",
prompt: "清理临时文件",
mode: "none",
session: "cron:cleanup-tmp" // Isolated
}
]
}
}
五、投递模式:结果分发策略
自动化任务的输出需要分发到不同渠道,OpenClaw提供三种投递模式。
5.1 投递模式对比
| 模式 | 行为 | 适用场景 | 配置示例 |
|---|---|---|---|
| announce | 生成摘要,投递到配置的Channel(如Telegram/Slack) | 需要人工感知的报告 | --mode announce |
| webhook | POST到指定URL(JSON Payload) | 系统集成、自动化流水线 | --mode webhook --url https://api.example.com/hook |
| none | 仅执行,无外部投递(日志保留) | 纯后台任务、敏感操作 | --mode none |
5.2 announce模式详解
默认投递到Agent绑定的主Channel,支持覆盖:
json5
{
cron: {
jobs: [
{
schedule: "0 0 9 * * 1-5",
prompt: "晨会准备:检查今日日程与待办",
mode: "announce",
announce: {
channel: "slack", // 覆盖默认Channel
target: "#daily-standup", // 指定频道/群组
format: "markdown" // 输出格式:markdown | text | json
}
}
]
}
}
摘要生成逻辑:
- Agent执行任务后,生成结构化摘要(标题、要点、行动项)
- 通过Channel适配器投递(自动适配Telegram/HTML/Markdown格式)
- 支持@mention提醒相关成员
5.3 webhook模式详解
适用于与外部系统集成,Payload结构标准化:
bash
# 配置Webhook投递
openclaw cron add "cron:0 */5 * * * *" \
--agent monitor \
--prompt "采集系统指标" \
--mode webhook \
--url "https://monitoring.example.com/metrics" \
--headers "Authorization:Bearer xxx,Content-Type:application/json"
Webhook Payload示例:
json
{
"jobId": "cron-123",
"timestamp": "2026-03-09T14:30:00Z",
"agentId": "monitor",
"sessionType": "isolated",
"result": {
"summary": "CPU使用率: 45%, 内存: 62%, 磁盘: 78%",
"status": "warning",
"details": [...],
"actions": ["建议清理磁盘"]
},
"metadata": {
"executionTimeMs": 1250,
"toolCalls": ["exec:df", "exec:free"]
}
}
重试策略:
- 首次失败:5秒后重试
- 二次失败:30秒后重试
- 三次失败:标记为失败,记录到cron runs日志
六、生产环境最佳实践
6.1 高频任务限流
秒级Cron任务需谨慎使用,避免耗尽API额度:
json5
{
cron: {
rateLimit: {
maxConcurrent: 3, // 最大并发任务数
perAgent: {
"monitor": { maxPerMinute: 10 }, // 监控Agent限流
"main": { maxPerMinute: 2 } // 主Agent限流
}
}
}
}
6.2 任务幂等性设计
对于可能重叠执行的任务,在Prompt中明确幂等性要求:
任务Prompt示例(幂等)
检查并清理/tmp目录下超过7天的日志文件。
注意:
- 先检查文件列表,避免误删
- 若文件已在上次执行时清理,直接返回"无需操作"
- 记录本次删除的文件名与大小,用于下次比对
6.3 故障通知
Heartbeat检查失败时,应升级通知:
json5
{
heartbeat: {
every: "30m",
mode: "announce",
onFailure: {
mode: "webhook",
url: "https://alerts.example.com/urgent",
escalateAfter: 3 // 连续3次失败升级告警
}
}
}
七、总结
OpenClaw的自动化编排体系通过六字段Cron引擎实现了从秒级到月级的精准调度,通过Heartbeat机制提供了可编排的健康巡检能力。两种执行模式(Main Session vs Isolated Session)的灵活选择,使得自动化任务既能融入用户对话上下文,又能保持后台任务的隔离性。
架构设计要点:
- 时间精度:六字段Cron支持秒级,适合高频监控场景
- 语义丰富:at/every/cron三种模式覆盖一次性、间隔、日历调度需求
- 执行隔离:Isolated Session通过cron:命名空间避免状态污染
- 投递灵活:announce/webhook/none三种模式适配人、系统、静默三种消费方
对于生产环境,建议将高频监控任务(秒级)与低频批处理(小时/天级)分离到不同Agent,利用工具权限控制与沙箱隔离,确保自动化编排的安全性与可观测性。
本文章基于OpenClaw官方文档学习撰写。仅供学习参考,请勿用于商业用途。