定时任务自动化实战

定时任务自动化实战

本指南介绍如何使用 OpenClaw 的 Cron 功能,打造各种自动化场景。

⏰ Cron 基础

Cron 表达式格式

分 时 日 月 周
*  *  *  *  *
│  │  │  │  │
│  │  │  │  └─ 星期几 (0-6, 0=周日)
│  │  │  └──── 月份 (1-12)
│  │  └─────── 日期 (1-31)
│  └────────── 小时 (0-23)
└───────────── 分钟 (0-59)

常用表达式

表达式说明
0 8 * * *每天 8:00
0 */6 * * *每 6 小时
0 9 * * 1-5工作日 9:00
0 0 * * 0每周日 00:00
*/30 * * * *每 30 分钟
0 8,12,18 * * *每天 8:00、12:00、18:00

🚀 快速开始

创建第一个定时任务

{
  automation: {
    cron: [
      {
        id: "daily-report",
        schedule: "0 9 * * 1-5",  // 工作日 9:00
        message: {
          to: "feishu:group-id",
          text: "早上好!新的一天开始了。",
        },
      },
    ],
  },
}

验证任务

# 查看所有定时任务
openclaw automation cron list

# 手动触发测试
openclaw automation cron trigger daily-report

# 查看下次执行时间
openclaw automation cron next

📋 实战场景

场景一:每日天气播报

{
  automation: {
    cron: [
      {
        id: "weather-report",
        schedule: "0 7 * * *",  // 每天 7:00
        message: {
          to: "feishu:group-id",
          text: "{{web.tools.web.search.query}}北京今天天气",
        },
      },
    ],
  },
}

优化版:使用 Web 工具获取实时天气

{
  automation: {
    cron: [
      {
        id: "weather-detailed",
        schedule: "0 7 * * *",
        tool: {
          name: "web",
          input: {
            url: "https://weather.com/weather/today/l/Beijing",
            extract: "temperature,condition",
          },
          then: {
            message: {
              to: "feishu:group-id",
              text: "今天北京:{{result.temperature}},{{result.condition}}",
            },
          },
        },
      },
    ],
  },
}

场景二:工作日报生成

{
  automation: {
    cron: [
      {
        id: "daily-summary",
        schedule: "0 18 * * 1-5",  // 工作日 18:00
        agent: "main",
        prompt: `
请根据今天的聊天记录和任务完成情况,生成一份日报,包含:
1. 今日完成的任务
2. 遇到的问题
3. 明日计划
`,
        message: {
          to: "feishu:my-group",
        },
      },
    ],
  },
}

场景三:定时提醒

{
  automation: {
    cron: [
      {
        id: "water-reminder",
        schedule: "0 */2 * * *",  // 每 2 小时
        message: {
          to: "telegram:my-chat-id",
          text: "💧 该喝水了!",
        },
      },
      {
        id: "stretch-reminder",
        schedule: "0 */4 * * *",  // 每 4 小时
        message: {
          to: "telegram:my-chat-id",
          text: "🧘 起来活动一下!",
        },
      },
    ],
  },
}

场景四:定时数据备份

{
  automation: {
    cron: [
      {
        id: "backup-config",
        schedule: "0 2 * * *",  // 凌晨 2:00
        command: "openclaw backup create --output ~/Backups",
        then: {
          webhook: {
            url: "https://your-server.com/backup-complete",
            method: "POST",
            body: {
              backup: "completed",
              timestamp: "{{now}}",
            },
          },
        },
      },
    ],
  },
}

场景五:定期健康检查

{
  automation: {
    cron: [
      {
        id: "health-check",
        schedule: "0 */6 * * *",  // 每 6 小时
        command: "openclaw doctor",
        then: {
          condition: "{{exitCode}} != 0",
          message: {
            to: "telegram:admin-id",
            text: "⚠️ 健康检查失败,请检查日志!",
          },
        },
      },
    ],
  },
}

🎯 高级技巧

动态时间

使用相对时间:

{
  automation: {
    cron: [
      {
        id: "weekend-start",
        schedule: "0 18 * * 5",  // 周五 18:00
        message: {
          text: "周末快乐!记得休息 🎉",
        },
      },
    ],
  },
}

条件执行

只在特定条件下执行:

{
  automation: {
    cron: [
      {
        id: "work-day-report",
        schedule: "0 18 * * 1-5",
        condition: {
          env: {
            key: "WORK_MODE",
            value: "active",
          },
        },
        message: {
          to: "feishu:work-group",
          text: "下班了,今日工作总结...",
        },
      },
    ],
  },
}

多目标发送

同时发送到多个渠道:

{
  automation: {
    cron: [
      {
        id: "broadcast",
        schedule: "0 12 * * *",
        message: [
          {
            to: "feishu:group-a",
            text: "午休时间到了!",
          },
          {
            to: "telegram:channel-b",
            text: "Lunch time! 🍜",
          },
        ],
      },
    ],
  },
}

Agent 增强

让 AI 生成更智能的消息:

{
  automation: {
    cron: [
      {
        id: "smart-reminder",
        schedule: "0 9 * * *",
        agent: "main",
        prompt: `
根据以下信息生成一条早安消息:
- 今天是 {{date}}
- 星期 {{weekday}}
- 天气:{{weather}}

要求:
1. 语气友好
2. 包含天气建议
3. 提醒今日重点事项
`,
        message: {
          to: "feishu:group-id",
        },
      },
    ],
  },
}

🔧 管理命令

# 列出所有任务
openclaw automation cron list

# 添加任务
openclaw automation cron add --id "new-task" --schedule "0 8 * * *" --message "Hello"

# 删除任务
openclaw automation cron remove "new-task"

# 暂停任务
openclaw automation cron disable "new-task"

# 恢复任务
openclaw automation cron enable "new-task"

# 查看任务日志
openclaw automation cron logs "new-task" --tail 50

⚠️ 注意事项

时区问题

Cron 使用系统时区,确认当前时区:

# 查看当前时区
date

# 设置时区(示例)
sudo timedatectl set-timezone Asia/Shanghai

任务重叠

如果任务执行时间超过间隔,可能会出现重叠:

{
  automation: {
    cron: [
      {
        id: "long-task",
        schedule: "*/5 * * * *",  // 每 5 分钟
        noOverlap: true,  // 防止重叠
        command: "long-running-task",
      },
    ],
  },
}

错误处理

配置失败时的行为:

{
  automation: {
    cron: [
      {
        id: "task-with-fallback",
        schedule: "0 8 * * *",
        retry: {
          maxAttempts: 3,
          backoff: "exponential",
        },
        onFailure: {
          message: {
            to: "telegram:admin",
            text: "任务执行失败:{{error}}",
          },
        },
      },
    ],
  },
}

🔗 相关文档

💡 最佳实践

  1. 测试先行:用短间隔测试,确认正常后再改回目标时间
  2. 添加日志:每个任务记录执行日志,便于排障
  3. 设置告警:关键任务失败时及时通知
  4. 避免重叠:长时间任务使用 noOverlap 保护
  5. 时区统一:所有任务使用统一的时区设置