定时任务(Cron)
通过自然语言或 Cron 表达式自动调度任务。Hermes 通过一个统一的 cronjob 工具暴露 Cron 管理功能,采用操作式指令风格,而非分离的“创建/列出/删除”工具。
当前 Cron 可实现的功能
Cron 任务可:
- 调度一次性或重复性任务
- 暂停、恢复、编辑、触发和移除任务
- 为任务附加零个、一个或多个技能
- 将结果返回至原始聊天会话、本地文件或已配置的平台目标
- 在全新的代理会话中运行,并使用标准静态工具列表
Cron 执行会话无法递归创建更多 Cron 任务。Hermes 会在 Cron 执行过程中禁用 Cron 管理工具,以防止出现无限调度循环。
创建定时任务
在聊天中使用 /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",
)
这在希望定时代理继承可复用的工作流,而无需将完整技能文本嵌入 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 执行由网关守护进程处理。 网关每 60 秒触发一次调度器,对所有到期的任务在隔离的代理会话中执行。
hermes gateway install # Install as a user service
sudo hermes gateway install --system # Linux: boot-time system service for servers
hermes gateway # Or run in foreground
hermes cron list
hermes cron status
网关调度行为
每次 tick 时,Hermes 会:
- 从
~/.hermes/cron/jobs.json加载任务 - 检查
next_run_at是否与当前时间匹配 - 为每个到期任务启动一个全新的
AIAgent会话 - 可选地将一个或多个附加技能注入该新会话
- 运行提示词直至完成
- 交付最终响应
- 更新运行元数据及下次计划时间
位于 ~/.hermes/cron/.tick.lock 的文件锁可防止多个调度 tick 重叠执行同一组任务。
输出交付选项
调度任务时,需指定输出目的地:
| 选项 | 说明 | 示例 |
|---|---|---|
"origin" | 返回任务创建位置 | 消息平台默认设置 |
"local" | 仅保存到本地文件(~/.hermes/cron/output/) | CLI 默认设置 |
"telegram" | Telegram 主频道 | 使用 TELEGRAM_HOME_CHANNEL |
"telegram:123456" | 指定 Telegram 聊天 ID | 直接投递 |
"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 发送短信 | |
"homeassistant" | Home Assistant | |
"dingtalk" | DingTalk | |
"feishu" | Feishu/Lark | |
"wecom" | WeCom | |
"weixin" | Weixin(微信) | |
"bluebubbles" | BlueBubbles(iMessage) |
代理的最终响应将自动交付。您无需在 Cron 提示词中调用 send_message。
响应包装
默认情况下,交付的 Cron 输出会被包裹在标题和页脚中,以便接收者知道其来自定时任务:
Cronjob Response: Morning feeds
-------------
<agent output here>
Note: The agent cannot see this message, and therefore cannot respond to it.
如需交付原始代理输出而不带包装,请将 cron.wrap_response 设置为 false:
# ~/.hermes/config.yaml
cron:
wrap_response: false
静默抑制
如果代理的最终响应以 [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 minutes
或设置 HERMES_CRON_SCRIPT_TIMEOUT 环境变量。优先级顺序为:环境变量 → config.yaml → 默认 120 秒。
提供商恢复机制
Cron 任务继承您配置的备用提供方和凭证池轮换策略。当主 API 密钥被限流或提供方返回错误时,Cron 代理可:
- 降级至备用提供方:若您已在
config.yaml中配置了fallback_providers(或旧版fallback_model) - 切换至同一提供方的下一个凭证:在您的 凭证池 中轮换
这意味着高频运行或高峰时段运行的 Cron 任务更具韧性——单个被限流的密钥不会导致整个任务失败。
调度格式
代理的最终响应会自动交付,您不需要在 Cron 提示词中包含 send_message 来指向相同的目标。如果 Cron 运行调用了 send_message 到与调度器将要交付的目标完全一致的位置,Hermes 会跳过重复发送,并指示模型将用户可见内容放入最终响应中。仅当需要额外或不同目标时,才使用 send_message。
相对延迟(一次性任务)
30m → Run once in 30 minutes
2h → Run once in 2 hours
1d → Run once in 1 day
间隔(重复任务)
every 30m → Every 30 minutes
every 2h → Every 2 hours
every 1d → Every day
Cron 表达式
0 9 * * * → Daily at 9:00 AM
0 9 * * 1-5 → Weekdays at 9:00 AM
0 */6 * * * → Every 6 hours
30 8 1 * * → First of every month at 8:30 AM
0 0 * * 0 → Every Sunday at midnight
ISO 时间戳
2026-03-15T09:00:00 → One-time at March 15, 2026 9:00 AM
重复行为
| 调度类型 | 默认重复次数 | 行为 |
|---|---|---|
一次性(30m, 时间戳) | 1 | 仅运行一次 |
间隔(every 2h) | 无限制 | 持续运行直至被移除 |
| Cron 表达式 | 无限制 | 持续运行直至被移除 |
您可覆盖默认行为:
cronjob(
action="create",
prompt="...",
schedule="every 2h",
repeat=5,
)
程序化管理任务
面向代理的 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。
存储使用原子文件写入,确保中断写入不会留下部分写入的任务文件。
自包含提示词依然重要
Cron 任务在完全全新的代理会话中运行。提示词必须包含代理所需的一切信息,除非这些信息已由附加技能提供。
错误示例: "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 后门尝试或明显秘密泄露载荷的提示词将被阻止。