Appearance
OpenClaw定时任务配置:自动化你的工作流程
定时任务让OpenClaw可以在指定时间自动执行操作,实现工作流程自动化。本文介绍如何配置和管理定时任务。
定时任务应用场景
| 场景 | 示例 |
|---|---|
| 定时报告 | 每天早上生成日报 |
| 数据同步 | 每小时同步外部数据 |
| 提醒通知 | 定时发送提醒消息 |
| 清理任务 | 定期清理临时文件 |
| 监控检查 | 定时检查系统状态 |
快速开始
创建第一个定时任务
在OpenClaw中,进入「设置」→「定时任务」→「创建任务」。
基本配置:
yaml
name: 每日新闻摘要
schedule: "0 9 * * *" # 每天早上9点
action: generate_news_summary
enabled: trueCron表达式说明
Cron表达式格式:秒 分 时 日 月 周
| 字段 | 取值范围 | 示例 |
|---|---|---|
| 秒 | 0-59 | 0 |
| 分 | 0-59 | 30 |
| 时 | 0-23 | 9 |
| 日 | 1-31 | * |
| 月 | 1-12 | * |
| 周 | 0-6 | 1-5 |
常用表达式:
| 表达式 | 说明 |
|---|---|
0 9 * * * | 每天早上9点 |
0 */2 * * * | 每2小时 |
0 9 * * 1-5 | 工作日早上9点 |
0 0 1 * * | 每月1号 |
0 30 8 * * * | 每天8:30 |
任务配置详解
基本配置
yaml
tasks:
- name: 任务名称
description: 任务描述
schedule: "0 9 * * *"
enabled: true
action:
type: prompt # prompt | script | api
content: "请生成今日新闻摘要"提示词任务
让AI在指定时间执行提示词:
yaml
tasks:
- name: 每日计划提醒
schedule: "0 8 * * 1-5"
action:
type: prompt
content: |
请根据以下信息生成今日工作计划:
- 昨日完成事项:{yesterday_tasks}
- 今日会议:{today_meetings}
- 待办事项:{todo_list}
output:
type: notification
title: "今日工作计划"脚本任务
执行自定义脚本:
yaml
tasks:
- name: 数据备份
schedule: "0 2 * * *"
action:
type: script
path: /scripts/backup.sh
env:
BACKUP_DIR: /backup
DATE: "{date}"API调用任务
调用外部API:
yaml
tasks:
- name: 同步数据
schedule: "0 */4 * * *"
action:
type: api
method: POST
url: https://api.example.com/sync
headers:
Authorization: "Bearer {api_token}"
body:
source: openclaw高级配置
条件执行
只在满足条件时执行:
yaml
tasks:
- name: 工作日提醒
schedule: "0 9 * * *"
condition:
weekday: [1, 2, 3, 4, 5] # 周一到周五
not_holiday: true
action:
type: prompt
content: "早上好!开始今天的工作吧!"重试机制
任务失败时自动重试:
yaml
tasks:
- name: 数据同步
schedule: "0 */2 * * *"
retry:
max_attempts: 3
interval: 300 # 5分钟后重试
backoff: exponential任务依赖
设置任务执行顺序:
yaml
tasks:
- name: 收集数据
schedule: "0 8 * * *"
id: collect_data
- name: 生成报告
schedule: "0 9 * * *"
depends_on: collect_data
action:
type: prompt
content: "根据收集的数据生成报告"任务管理
查看任务列表
bash
openclaw tasks list启用/禁用任务
bash
# 启用任务
openclaw tasks enable <task-name>
# 禁用任务
openclaw tasks disable <task-name>手动执行任务
bash
openclaw tasks run <task-name>查看执行历史
bash
openclaw tasks history <task-name>执行监控
日志记录
yaml
tasks:
- name: 重要任务
schedule: "0 9 * * *"
logging:
enabled: true
level: debug
file: /logs/task.log通知配置
任务执行后发送通知:
yaml
tasks:
- name: 日报生成
schedule: "0 18 * * *"
notify:
on_success:
- type: email
to: team@example.com
subject: "日报已生成"
on_failure:
- type: webhook
url: https://hooks.example.com/alert监控指标
yaml
monitoring:
metrics:
- execution_time
- success_rate
- error_count
alerts:
- condition: "success_rate < 0.9"
action: notify_admin常见用例
每日工作总结
yaml
tasks:
- name: 每日工作总结
schedule: "0 18 * * 1-5"
action:
type: prompt
content: |
请根据今天的对话记录,生成工作总结:
- 完成的任务
- 遇到的问题
- 明天的计划
output:
type: file
path: /reports/daily/{date}.md定时数据备份
yaml
tasks:
- name: 数据库备份
schedule: "0 2 * * *"
action:
type: script
path: /scripts/backup_db.sh
notify:
on_success:
- type: log
message: "备份完成"周报生成
yaml
tasks:
- name: 周报生成
schedule: "0 17 * * 5" # 周五下午5点
action:
type: prompt
content: |
请根据本周的日报,生成周报:
{daily_reports}
output:
type: email
to: manager@example.com
subject: "本周工作周报"最佳实践
1. 合理设置时间
避免在高峰期执行资源密集型任务。
2. 添加错误处理
yaml
tasks:
- name: 重要任务
error_handling:
on_failure: notify
retry: true3. 定期检查任务
定期检查任务执行情况,确保正常运行。
4. 保留执行日志
便于排查问题和审计。
总结
定时任务让OpenClaw可以:
- 自动执行重复性工作
- 在指定时间推送提醒
- 定期生成报告
- 自动化数据同步
合理使用定时任务,可以大大提高工作效率。建议从简单任务开始,逐步增加复杂度。
