定时任务 (Cron Scheduler)
> 📖 本文档翻译自 Hermes Agent 官方文档 > 最后更新:2026-04-16
通过自然语言或 cron 表达式自动调度任务。Hermes 通过单个 cronjob 工具暴露 cron 管理,使用动作风格的命令操作,而不是独立的 schedule/list/remove 工具。
Cron 的功能
Cron 任务可以:
- 调度一次性或重复性任务
- 暂停、恢复、编辑、触发和删除任务
- 为任务附加零个、一个或多个技能
- 将结果投递回原始聊天、本地文件或配置的平台目标
- 在新的 Agent 会话中使用正常的静态工具列表运行
:::warning
:::
创建定时任务
在聊天中使用 /cron
/cron add 30m "Remind me to check the build"
/cron add "every 2h" "Check server status"
/cron add "every 1h" "Summarize new feed items" --skill blogwatcher
/cron add "every 1h" "Use both skills and combine the result" --skill blogwatcher --skill find-nearby
通过独立 CLI
hermes cron create "every 2h" "Check server status"
hermes cron create "every 1h" "Summarize new feed items" --skill blogwatcher
hermes cron create "every 1h" "Use both skills and combine the result" \
--skill blogwatcher \
--skill find-nearby \
--name "Skill combo"
通过自然对话
像平常一样请求 Hermes:
Every morning at 9am, check Hacker News for AI news and send me a summary on Telegram.
Hermes 会在内部使用统一的 cronjob 工具。
技能支持的 Cron 任务
Cron 任务可以在运行提示之前加载一个或多个技能。
单个技能
cronjob(
action="create",
skill="blogwatcher",
prompt="Check the configured feeds and summarize anything new.",
schedule="0 9 * * *",
name="Morning feeds",
)
多个技能
技能按顺序加载。提示成为在这些技能之上的任务指令。
cronjob(
action="create",
skills=["blogwatcher", "find-nearby"],
prompt="Look for new local events and interesting nearby places, then combine them into one short brief.",
schedule="every 6h",
name="Local brief",
)
当你希望定时 Agent 继承可复用的工作流,而不必将完整的技能文本塞入 cron 提示时,这很有用。
编辑任务
你不需要删除并重新创建任务就可以更改它们。
聊天
/cron edit <job_id> --schedule "every 4h"
/cron edit <job_id> --prompt "Use the revised task"
/cron edit <job_id> --skill blogwatcher --skill find-nearby
/cron edit <job_id> --remove-skill blogwatcher
/cron edit <job_id> --clear-skills
独立 CLI
hermes cron edit <job_id> --schedule "every 4h"
hermes cron edit <job_id> --prompt "Use the revised task"
hermes cron edit <job_id> --skill blogwatcher --skill find-nearby
hermes cron edit <job_id> --add-skill find-nearby
hermes cron edit <job_id> --remove-skill blogwatcher
hermes cron edit <job_id> --clear-skills
注意:
- 重复的
--skill会替换任务附加的技能列表 --add-skill追加到现有列表而不替换--remove-skill移除特定的附加技能--clear-skills移除所有附加技能
生命周期操作
Cron 任务现在拥有比单纯的创建/删除更完整的生命周期。
聊天
/cron list
/cron pause <job_id>
/cron resume <job_id>
/cron run <job_id>
/cron remove <job_id>
独立 CLI
hermes cron list
hermes cron pause <job_id>
hermes cron resume <job_id>
hermes cron run <job_id>
hermes cron remove <job_id>
hermes cron status
hermes cron tick
各操作说明:
pause— 保留任务但停止调度resume— 重新启用任务并计算下一次运行时间run— 在下一次调度器滴答时触发任务remove— 完全删除任务
工作原理
Cron 执行由 Gateway 守护进程处理。 Gateway 每 60 秒滴答一次调度器,在隔离的 Agent 会话中运行所有到期任务。
hermes gateway install # 安装为用户服务
sudo hermes gateway install --system # Linux:服务器启动时的系统服务
hermes gateway # 或在前台运行
hermes cron list
hermes cron status
Gateway 调度器行为
每次滴答时 Hermes 会:
- 从
~/.hermes/cron/jobs.json加载任务 - 检查
next_run_at是否小于等于当前时间 - 为每个到期任务启动一个全新的
AIAgent会话 - 可选地将一个或多个附加技能注入到该新会话中
- 运行提示至完成
- 投递最终响应
- 更新运行元数据和下一次调度时间
~/.hermes/cron/.tick.lock 处的文件锁防止重叠的调度器滴答重复运行同一批任务。
投递选项
调度任务时,你可以指定输出去向:
| 选项 | 描述 | 示例 |
|---|---|---|
| "origin" | 回到任务创建的位置 | 消息平台上的默认值 |
| "local" | 仅保存到本地文件(~/.hermes/cron/output/) | CLI 上的默认值 |
| "telegram" | Telegram 主频道 | 使用 TELEGRAM_HOME_CHANNEL |
| "telegram:123456" | 按 ID 指定的 Telegram 聊天 | 直接投递 |
| "telegram:-100123:17585" | 特定 Telegram 话题 | chat_id:thread_id 格式 |
| "discord" | Discord 主频道 | 使用 DISCORD_HOME_CHANNEL |
| "discord:#engineering" | 特定 Discord 频道 | 按频道名称 |
| "slack" | Slack 主频道 | |
| "whatsapp" | WhatsApp 主页 | |
| "signal" | Signal | |
| "matrix" | Matrix 主房间 | |
| "mattermost" | Mattermost 主频道 | |
| "email" | 电子邮件 | |
| "sms" | 通过 Twilio 的 SMS | |
| "homeassistant" | Home Assistant | |
| "dingtalk" | 钉钉 | |
| "feishu" | 飞书/Lark | |
| "wecom" | 企业微信 | |
| "weixin" | 微信 | |
| "bluebubbles" | BlueBubbles(iMessage) | |
| "qqbot" | QQ Bot(腾讯 QQ) |
Agent 的最终响应会自动投递。你不需要在 cron 提示中调用 send_message。
响应包装
默认情况下,投递的 cron 输出会包装一个页眉和页脚,让接收者知道它来自定时任务:
Cronjob Response: Morning feeds
-------------
<agent output here>
Note: The agent cannot see this message, and therefore cannot respond to it.
要投递不带包装的原始 Agent 输出,将 cron.wrap_response 设置为 false:
# ~/.hermes/config.yaml
cron:
wrap_response: false
静默抑制
如果 Agent 的最终响应以 [SILENT] 开头,投递将被完全抑制。输出仍会保存在本地用于审计(在 ~/.hermes/cron/output/ 中),但不会向投递目标发送任何消息。
这对于只应在出现问题时报告的监控任务很有用:
Check if nginx is running. If everything is healthy, respond with only [SILENT].
Otherwise, report the issue.
失败的任务无论是否有 [SILENT] 标记都会投递——只有成功的运行可以被静默。
脚本超时
预运行脚本(通过 script 参数附加)的默认超时为 120 秒。如果你的脚本需要更长时间——例如包含随机延迟以避免机器人般的定时模式——可以增加此值:
# ~/.hermes/config.yaml
cron:
script_timeout_seconds: 300 # 5 分钟
或设置 HERMES_CRON_SCRIPT_TIMEOUT 环境变量。解析顺序为:环境变量 → config.yaml → 120 秒默认值。
Provider 恢复
Cron 任务继承你配置的回退 Provider 和凭据池轮换。如果主 API Key 被限速或 Provider 返回错误,cron Agent 可以:
- 回退到备用 Provider——如果你在
config.yaml中配置了fallback_providers(或旧版的fallback_model) - 轮换到同一 Provider 的下一个凭据——在你的凭据池中
这意味着高频率或在高峰时段运行的 cron 任务更有弹性——单个被限速的 Key 不会导致整个运行失败。
调度格式
Agent 的最终响应会自动投递——你不需要在 cron 提示中为同一目标包含 send_message。如果 cron 运行调用 send_message 到调度器已经投递的完全相同的目标,Hermes 会跳过该重复发送并告诉模型将面向用户的内容放在最终响应中。仅对额外或不同的目标使用 send_message。
相对延迟(一次性)
30m → 在 30 分钟后运行一次
2h → 在 2 小时后运行一次
1d → 在 1 天后运行一次
间隔(重复)
every 30m → 每 30 分钟
every 2h → 每 2 小时
every 1d → 每天
Cron 表达式
0 9 * * * → 每天上午 9:00
0 9 * * 1-5 → 工作日上午 9:00
0 */6 * * * → 每 6 小时
30 8 1 * * → 每月 1 日上午 8:30
0 0 * * 0 → 每周日凌晨
ISO 时间戳
2026-03-15T09:00:00 → 2026 年 3 月 15 日上午 9:00 一次性运行
重复行为
| 调度类型 | 默认重复 | 行为 |
|---|---|---|
| 一次性(30m、时间戳) | 1 | 运行一次 |
| 间隔(every 2h) | forever | 运行直到被删除 |
| Cron 表达式 | forever | 运行直到被删除 |
你可以覆盖它:
cronjob(
action="create",
prompt="...",
schedule="every 2h",
repeat=5,
)
编程式管理任务
面向 Agent 的 API 是一个工具:
cronjob(action="create", ...)
cronjob(action="list")
cronjob(action="update", job_id="...")
cronjob(action="pause", job_id="...")
cronjob(action="resume", job_id="...")
cronjob(action="run", job_id="...")
cronjob(action="remove", job_id="...")
对于 update,传入 skills=[] 来移除所有附加技能。
任务存储
任务存储在 ~/.hermes/cron/jobs.json 中。任务运行的输出保存在 ~/.hermes/cron/output/{job_id}/{timestamp}.md。
存储使用原子文件写入,因此中断的写入不会留下部分写入的任务文件。
自包含的提示仍然重要
:::warning
:::
糟糕: "Check on that server issue"
好: "SSH into server 192.168.1.100 as user 'deploy', check if nginx is running with 'systemctl status nginx', and verify https://example.com returns HTTP 200."
安全
定时任务提示在创建和更新时会扫描提示注入和凭据泄露模式。包含不可见 Unicode 技巧、SSH 后门尝试或明显的秘密泄露负载的提示会被阻止。