CLAWIST.
🟡 中级阅读时间 12 分钟••作者:Lin6

使用 OpenClaw Cron 任务实现自动化:完整指南

OpenClaw 网站

AI 助手真正的力量不仅在于响应请求,更在于代表您自主工作。OpenClaw's 的 cron 系统让您可以安排无需任何人工干预的周期性任务,将您的代理变成真正的自动化引擎。

本指南将向您展示如何在 OpenClaw 中创建、管理和优化 cron 任务,涵盖从每日内容生成到每小时监控任务的各种场景。

了解 OpenClaw Cron 任务

OpenClaw cron 任务是 按计划运行的隔离代理会话 。与执行脚本的传统 cron 任务不同,OpenClaw cron 会启动一个完整的 AI 代理会话,可访问所有工具——浏览器自动化、文件操作、API 调用和外部集成。

每个 cron 任务都在各自独立的上下文中运行,避免干扰您的主对话会话,也避免因累积的对话历史而导致上下文溢出。

Cron 与 Heartbeat 的对比

OpenClaw 有两种自动化机制:

Heartbeat(心跳): 在主代理会话中进行的定期检查。最适合:

  • 一次性综合检查多项内容(在一轮中同时检查收件箱和日历)
  • 需要对话上下文的任务
  • 时间灵活(大约每 30 分钟一次即可)

Cron 任务: 隔离的定时任务。最适合:

  • 有精确时间要求的任务("每周一上午 9 点整")
  • 内容生成或繁重处理
  • 一次性提醒
  • 无需主会话参与、直接向渠道发送结果的任务

经验法则: 对于精确的时间安排和独立任务,请使用 cron。对于允许略有偏差的批量检查,请使用 heartbeat。

创建您的第一个 Cron 任务

让我们从一个简单的示例开始:每日早间简报。

使用 CLI

# Create a daily briefing at 7 AM
openclaw cron add daily-briefing \
  --schedule "0 7 * * *" \
  --task "Generate morning briefing: check weather, calendar for today, and top 3 news headlines. Format as concise summary."

The 参数使用标准 cron 语法: ┌───────────── minute (0 - 59) │ ┌───────────── hour (0 - 23) │ │ ┌───────────── day of month (1 - 31) │ │ │ ┌───────────── month (1 - 12) │ │ │ │ ┌───────────── day of week (0 - 6, Sunday = 0) │ │ │ │ │ 0 7 * * *

使用配置文件

对于更复杂的设置,请编辑

~/.openclaw/config.yaml ~/.openclaw/config.yaml:

cron:
  - id: daily-briefing
    schedule: "0 7 * * *"
    task: |
      生成晨间简报:
      1. 查看今日天气
      2. 列出未来24小时的日历事件
      3. 搜索前3条科技新闻头条
      4. 整理为简明摘要并通过 Discord 发送
    channel: discord
    channelTarget: "@Shaun"
    model: claude-sonnet-4
    thinking: low

编辑配置后,请重新加载:

openclaw gateway restart

常见的 Cron 模式

每日内容创作

自动生成博客文章:

- id: chaf-growth-engine
  schedule: "0 9 * * *"  # 每天上午9点
  task: |
    CHAF Growth Engine 每日运行:
    1. 从 Twitter、Reddit、HackerNews 找出20个热门 AI/LLM 话题
    2. 围绕这些话题撰写20篇博客文章(每篇1000字以上)
    3. 通过 git commit 和 push 部署到 claw.ist
    4. 将网址提交到 Google Search Console
    5. 向 Discord 汇报摘要
  channel: discord
  channelTarget: "#growth"
  model: claude-opus-4
  thinking: high

每小时监控

每小时检查系统状态:

- id: system-health-check
  schedule: "0 * * * *"  # 每小时
  task: |
    检查系统健康状况:
    - 磁盘空间(使用率 >80% 时警告)
    - 内存使用率(>90% 时警告)
    - 网关状态
    - 最近的错误日志
    仅在发现问题时通知,否则保持静默。
  channel: discord
  channelTarget: "@Shaun"

每周报告

生成分析摘要:

- id: weekly-analytics
  schedule: "0 9 * * 1"  # 每周一上午9点
  task: |
    生成每周分析报告:
    1. 获取 claw.ist 的 Google Analytics 数据(过去7天)
    2. 与上一周进行比较
    3. 找出表现最好的10篇文章
    4. 在 Google Search Console 中查看新收录的页面
    5. 整理为包含洞察的报告并发送到邮箱
  channel: email
  channelTarget: "shaun@example.com"

每月维护

每月归档旧数据:

- id: monthly-cleanup
  schedule: "0 2 1 * *"  # 每月1日凌晨2点
  task: |
    每月维护:
    1. 将超过90天的记忆日志归档到 `memory/archive/`
    2. 压缩旧截图
    3. 清理临时文件
    4. 生成存储使用情况报告
    5. 将归档提交到 git
  model: claude-sonnet-4

一次性提醒

安排未来的一次性任务:

# 2小时后提醒我
openclaw cron add remind-meeting \
  --schedule "$(date -d '+2 hours' '+%M %H %d %m *')" \
  --task "Reminder: Team meeting in 15 minutes. Review meeting notes in ~/Documents/meeting-prep.md" \
  --once

高级 Cron 功能

模型选择

不同的任务需要不同的模型:

- id: quick-check
  schedule: "*/15 * * * *"  # 每15分钟
  task: "Check for urgent emails"
  model: claude-haiku-4  # 最快、最便宜

- id: deep-analysis
  schedule: "0 9 * * 1"  # 每周
  task: "Analyze competitor strategies and write detailed report"
  model: claude-opus-4  # 质量最佳

思考级别

控制推理深度:

- id: simple-task
  schedule: "0 * * * *"
  task: "Post current time to Discord"
  thinking: off  # 无需扩展思考

- id: complex-task
  schedule: "0 9 * * *"
  task: "Design marketing strategy for new product launch"
  thinking: high  # 启用完整推理

渠道路由

将输出发送到不同的目的地:

- id: public-post
  schedule: "0 12 * * *"
  task: "Generate motivational quote and image"
  channel: twitter
  channelTarget: "@YourHandle"

- id: private-alert
  schedule: "0 */6 * * *"
  task: "Check server health"
  channel: discord
  channelTarget: "@Shaun"

环境变量

向 cron 任务传递动态数据:

- id: context-aware-task
  schedule: "0 8 * * *"
  task: "Generate daily briefing for ${USER_LOCATION}"
  env:
    USER_LOCATION: "San Francisco"
    WEATHER_UNITS: "fahrenheit"

管理 Cron 任务

列出所有任务

openclaw cron list

输出:

ID                  Schedule       Next Run             Status
daily-briefing      0 7 * * *      2026-02-25 07:00    enabled
system-health       0 * * * *      2026-02-24 20:00    enabled
weekly-analytics    0 9 * * 1      2026-02-26 09:00    enabled

查看任务详情

openclaw cron show daily-briefing

禁用/启用任务

# 暂时禁用
openclaw cron disable daily-briefing

# 重新启用
openclaw cron enable daily-briefing

删除任务

openclaw cron remove daily-briefing

查看执行历史

# 显示最近 10 次运行
openclaw cron history daily-briefing --limit 10

# 显示今天'的运行记录
openclaw cron history daily-briefing --date 2026-02-24

手动触发

无需等待即可测试定时任务:

openclaw cron run daily-briefing

最佳实践

1. 保持任务专注

错误示范: 一个包揽所有事情的定时任务

task: "Check email, calendar, weather, news, generate blog post, update analytics, clean files, etc."

正确示范: 多个专注的定时任务

- id: morning-brief
  task: "Weather + calendar for today"
- id: content-gen
  task: "Write daily blog post"
- id: cleanup
  task: "Archive old files"

2. 使用合适的模型

# 快速检查:Haiku(快速、低成本)
- task: "Check for new emails"
  model: claude-haiku-4

# 日常工作:Sonnet(均衡)
- task: "Generate daily blog post"
  model: claude-sonnet-4

# 复杂分析:Opus(最佳质量)
- task: "Competitive research and strategy"
  model: claude-opus-4

3. 验证定时任务的输出

关键提示: 对于创建内容的定时任务,务必验证输出:

- id: blog-publisher
  task: |
    1. Write 5 blog posts
    2. Deploy to claw.ist
    3. VERIFY each post loads correctly via browser
    4. Only report success after verification passes
    5. If verification fails, report the failures

未经验证,切勿相信定时任务自行报告的"完成"状态。

4. 优雅地处理失败

task: |
  Try to fetch analytics data.
  If API fails, retry twice with 30s delay.
  If still failing, send error report but don't crash.
  Include error details and timestamp in report.

5. 避开高峰时段

将繁重的任务安排在非高峰时段:

# 推荐:凌晨 2 点进行繁重处理
- id: data-backup
  schedule: "0 2 * * *"
  task: "Full database backup and compression"

# 不推荐:上午 9 点,此时人们需要系统保持响应
- id: heavy-scrape
  schedule: "0 9 * * *"
  task: "Scrape 1000 websites"

6. 记录重要事件

task: |
  Generate weekly report.
  Save report to ~/reports/weekly-$(date +%Y-%m-%d).md
  Log completion to memory/cron-activity.log
  Send summary to Discord.

7. 使用描述性的 ID

# 好的 ID
- id: daily-briefing-7am
- id: weekly-analytics-monday
- id: hourly-health-check

# 不好的 ID
- id: task1
- id: cron-job
- id: test

实际案例

内容流水线

- id: youtube-to-blog
  schedule: "0 10 * * *"
  task: |
    Daily YouTube content pipeline:
    1. Search YouTube for "AI tutorials" uploaded in last 24h
    2. Select top 5 videos by views
    3. Download transcripts using yt-dlp
    4. Write blog posts based on transcripts (1000+ words each)
    5. Add proper frontmatter and code examples
    6. Save to ~/workspace/clawist/content/posts/
    7. Git commit and push to deploy
    8. Submit URLs to Google Search Console
    9. Verify posts load correctly
    10. Report results to Discord
  model: claude-opus-4
  thinking: high
  channel: discord
  channelTarget: "#content-pipeline"

邮件自动化

- id: inbox-zero
  schedule: "0 */3 * * *"  # Every 3 hours
  task: |
    Email management:
    1. Check inbox for new messages
    2. Categorize: urgent, action-needed, informational, spam
    3. Draft replies for simple questions
    4. Flag urgent items for human review
    5. Archive informational emails
    6. Notify about urgent emails only
  channel: discord
  channelTarget: "@Shaun"

社交媒体调度器

- id: twitter-scheduler
  schedule: "0 9,13,17 * * *"  # 9 AM, 1 PM, 5 PM
  task: |
    Post to Twitter:
    1. Read ~/social-queue.md for scheduled posts
    2. Pick next post for this time slot
    3. Generate image if needed
    4. Post to Twitter
    5. Mark as posted in queue file
    6. Log result
  channel: twitter

数据收集

- id: competitor-monitor
  schedule: "0 8 * * *"
  task: |
    监控竞争对手:
    1. 通过浏览器访问竞争对手网站
    2. 检查是否有新博客文章、产品更新
    3. 对任何变化进行截图
    4. 与昨天的快照进行对比
    5. 如果发现重大变化,进行分析并报告
    6. 更新跟踪表格
  model: claude-sonnet-4

故障排查

Cron 无法运行

# 检查 cron 是否已启用
openclaw cron list

# 检查 Gateway 是否正在运行
openclaw gateway status

# 查看 Gateway 日志
openclaw gateway logs | grep cron

# 验证调度语法
# 使用 https://crontab.guru/ 进行验证

未收到输出

检查渠道配置:

# 验证是否已设置 Discord 令牌
openclaw config get message.discord.token

# 手动测试渠道
openclaw message send --channel discord --target "@Shaun" --message "Test"

任务静默失败

添加明确的错误处理:

task: |
  try:
    [你的任务]
  catch error:
    将错误及完整详情报告到 Discord
    记录到 ~/cron-errors.log

API 成本过高

# 检查哪些 cron 运行最频繁
openclaw cron list

# 对频繁执行的任务切换到更轻量的模型
# Haiku:约 $0.25/百万 token
# Sonnet:约 $3/百万 token
# Opus:约 $15/百万 token

下一步

现在你已经了解了 OpenClaw 的 cron 任务:

  1. 从简单开始: 创建一个每日简报 cron
  2. 监控执行情况: 检查日志并验证输出
  3. 逐步扩展: 随着需求出现,再添加更多 cron
  4. 优化成本: 为每个任务选用合适的模型
  5. 构建工作流: 将多个 cron 串联起来

你的 AI 助手现在可以全天候自动运行——生成内容、监控系统、处理日常任务,让你专注于真正重要的事情。

祝你自动化愉快!🤖⏰