跳到主要內容

定時任務(Cron)

使用自然語言或 Cron 表達式自動調度任務。Hermes 通過單一的 cronjob 工具提供 Cron 管理功能,採用操作式(action-style)命令,而非分離的 schedule/list/remove 工具。

當前 Cron 的功能

Cron 任務可以:

  • 調度一次性或重複性任務
  • 暫停、恢復、編輯、觸發和刪除任務
  • 為任務附加零個、一個或多個技能(skills)
  • 將結果返回到原始聊天會話、本地文件或已配置的平臺目標
  • 在全新的 Agent 會話中運行,使用正常的靜態工具列表
注意

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",
)

這在希望定時 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 daemon)處理。 網關每 60 秒觸發一次調度器,對所有到期的任務在隔離的 Agent 會話中運行。

hermes gateway install     # 安裝為用戶服務
sudo hermes gateway install --system # Linux:服務器的啟動時系統服務
hermes gateway # 或者在前臺運行

hermes cron list
hermes cron status

網關調度器行為

每次觸發時,Hermes 執行以下步驟:

  1. ~/.hermes/cron/jobs.json 加載任務
  2. next_run_at 與當前時間進行比較
  3. 為每個到期任務啟動一個全新的 AIAgent 會話
  4. 可選地將一個或多個附加技能注入該新會話
  5. 運行提示直至完成
  6. 交付最終響應
  7. 更新運行元數據和下次計劃時間

文件鎖 ~/.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 發送短信
"homeassistant"Home Assistant
"dingtalk"釘釘
"feishu"飛書/飛書
"wecom"企業微信
"weixin"微信(WeChat)
"bluebubbles"BlueBubbles(iMessage)

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 秒默認值。

提供商恢復

定時任務繼承您配置的備用提供方和憑證池輪換策略。如果主 API 密鑰被限流或提供方返回錯誤,定時 Agent 可以:

  • 回退到備用提供方(如果您在 config.yaml 中配置了 fallback_providers 或舊版的 fallback_model
  • 切換到同一提供方的下一個憑證,使用您的 憑證池

這意味著在高頻率運行或高峰時段執行的定時任務更具容錯能力——單個被限流的密鑰不會導致整個任務失敗。

計劃格式

Agent 的最終響應會自動發送——您不需要在定時任務提示中包含 send_message 來發送到相同的目標。如果定時任務調用 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,
)

通過程序化方式管理任務

面向 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

存儲使用原子文件寫入,因此中斷的寫入不會留下部分寫入的任務文件。

自包含提示仍然重要

重要

定時任務在完全全新的 Agent 會話中運行。提示必須包含 Agent 所需的所有內容,除非這些內容已由附加技能提供。

錯誤示例: "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 後門嘗試或明顯憑證洩露載荷的提示將被阻止。