5 分钟一次心跳,自动化一切
什么是心跳?
心跳是 OpenClaw 的自动化机制:
每 5 分钟 → 检查任务 → 执行 → 记录结果
就像人的心跳一样,持续不断地工作。
能做什么?
1. 监控告警
heartbeat:
tasks:
- name: server-health
action: |
检查 CPU/内存/磁盘
超阈值发送 Telegram 告警
2. 定时任务
heartbeat:
tasks:
- name: daily-report
schedule: "0 18 * * *" # 每天 18:00
action: |
生成今日报告
发送到飞书
3. 数据同步
heartbeat:
tasks:
- name: sync-data
interval: 30m
action: |
从 API 拉取最新数据
更新本地数据库
4. 自动回复
heartbeat:
tasks:
- name: check-messages
action: |
检查未读消息
根据规则自动回复
配置示例
# ~/.openclaw/config.yaml
heartbeat:
enabled: true
interval: 5m
tasks:
# 任务 1:监控
- name: monitor-servers
action: ~/.openclaw/scripts/monitor.sh
on_failure: notify-admin
# 任务 2:日报
- name: daily-report
schedule: "0 18 * * *"
action: ~/.openclaw/scripts/report.py
# 任务 3:清理
- name: cleanup-logs
schedule: "0 0 * * 0" # 每周日 00:00
action: |
find ~/.openclaw/logs -mtime +30 -delete
高级用法
条件执行
heartbeat:
tasks:
- name: send-reminder
condition: "weekday && hour >= 9 && hour <= 18"
action: 发送工作提醒
链式任务
heartbeat:
tasks:
- name: fetch-and-analyze
actions:
- fetch_data_from_api
- analyze_with_ai
- send_report_to_telegram
失败重试
heartbeat:
tasks:
- name: important-task
action: critical_script.sh
retry: 3
retry_delay: 1m
心跳日志
所有心跳执行都会记录:
# 查看日志
openclaw logs --heartbeat --tail 100
# 输出示例
2026-03-12 12:40:00 [HEARTBEAT] monitor-servers: OK
2026-03-12 12:45:00 [HEARTBEAT] daily-report: Scheduled (18:00)
我的实战配置
# 我的 OpenClaw 心跳配置
heartbeat:
enabled: true
interval: 5m
tasks:
# 每 5 分钟:监控加密货币价格
- name: check-crypto-prices
action: python3 ~/scripts/crypto_monitor.py
on_alert: send-telegram
# 每小时:检查接单平台
- name: scan-platforms
schedule: "0 * * * *"
action: python3 ~/scripts/scan_platforms.py
# 每天 8:00:语音简报
- name: morning-briefing
schedule: "0 8 * * *"
action: python3 ~/scripts/briefing.py --voice
# 每天 18:00:工作总结
- name: daily-summary
schedule: "0 18 * * *"
action: python3 ~/scripts/summary.py
性能优化
1. 控制执行频率
# 不要设置太频繁
interval: 5m # 推荐,不要太短
2. 超时设置
tasks:
- name: slow-task
timeout: 30s # 防止卡住
3. 资源限制
# 限制并发
concurrency: 3
常见问题
Q: 心跳会消耗很多资源吗? A: 不会。每次心跳只是执行配置的任务,空闲时几乎不占资源。
Q: 心跳失败了怎么办?
A: 查看 openclaw logs,会记录失败原因。
Q: 可以暂停心跳吗?
A: 可以,openclaw heartbeat pause,恢复用 openclaw heartbeat resume。
心跳 vs Cron
| 维度 | OpenClaw 心跳 | Linux Cron |
|---|---|---|
| 配置 | YAML,简单 | 语法复杂 |
| 日志 | 自动记录 | 需手动配置 |
| AI 集成 | 原生支持 | 需自己写 |
| 多渠道通知 | 内置 | 需自己写 |
结论:OpenClaw 心跳更适合 AI 场景。
快速开始
# 启用心跳
openclaw heartbeat enable
# 测试任务
openclaw heartbeat test --task monitor-servers
# 查看状态
openclaw heartbeat status
⏱️ 你的 AI 助手有"心跳"吗?评论区分享你的自动化任务!
🎯 需要 OpenClaw 自动化配置服务?微信:yang1002378395