H繁中版
文件教學與最佳實踐automate with cron
<!-- Source: https://hermesbible.com/docs/guides/automate-with-cron -->

每日簡報機器人教學涵蓋了基礎知識。本指南會更深入——五個你可以套用到自己工作流程的真實自動化模式。

完整功能參考請見排程任務(Cron)

重點概念

Cron 工作在全新的代理人會話中運行,不會記住你當前的對話。提示必須完全自含——包含代理人需要知道的所有資訊。

小提示——不需要 LLM?你有兩個零 Token 的選項。

  • 定期看門狗腳本:如果腳本已經能產生精確的訊息(記憶體警報、磁碟警報、心跳),請使用純腳本 Cron 工作。相同的排程器,不使用 LLM。你可以在對話中要求 Hermes 幫你設定——cronjob 工具知道何時該選擇 no_agent=True 並為你撰寫腳本。
  • 從已運行的腳本一次性執行(CI 步驟、提交後鉤子、部署腳本、外部排程監控器):使用 hermes send 將 stdout 或檔案直接傳送到 Telegram / Discord / Slack 等,而不需要設定 Cron 條目。

模式一:網站變更監控器

監控某個網址的變更,只在有變動時通知你。

script 參數是這裡的秘密武器。Python 腳本會在每次執行前運行,其 stdout 會成為代理人的上下文。腳本負責機械性的工作(取得、比對);代理人負責推理(這個變更有意思嗎?)。

建立監控腳本:

mkdir -p ~/.hermes/scripts
import hashlib, json, os, urllib.request

URL = "https://example.com/pricing"
STATE_FILE = os.path.expanduser("~/.hermes/scripts/.watch-site-state.json")

# Fetch current content
req = urllib.request.Request(URL, headers={"User-Agent": "Hermes-Monitor/1.0"})
content = urllib.request.urlopen(req, timeout=30).read().decode()
current_hash = hashlib.sha256(content.encode()).hexdigest()

# Load previous state
prev_hash = None
if os.path.exists(STATE_FILE):
    with open(STATE_FILE) as f:
        prev_hash = json.load(f).get("hash")

# Save current state
with open(STATE_FILE, "w") as f:
    json.dump({"hash": current_hash, "url": URL}, f)

# Output for the agent
if prev_hash and prev_hash != current_hash:
    print(f"CHANGE DETECTED on {URL}")
    print(f"Previous hash: {prev_hash}")
    print(f"Current hash: {current_hash}")
    print(f"\nCurrent content (first 2000 chars):\n{content[:2000]}")
else:
    print("NO_CHANGE")

設定 Cron 工作:

/cron add "every 1h" "If the script output says CHANGE DETECTED, summarize what changed on the page and why it might matter. If it says NO_CHANGE, respond with just [SILENT]." --script ~/.hermes/scripts/watch-site.py --name "Pricing monitor" --deliver telegram

小提示——[SILENT] 技巧

對於 Cron 監控工作,指示代理人在沒有變更時只回覆 [SILENT]。Cron 傳遞機制會將 [SILENT] 視為靜默標記,因此你只會在實際發生事情時收到通知——安靜時段不會有垃圾訊息。


模式二:每週報告

從多個來源彙整資訊並生成格式化的摘要。每週運行一次,傳送到你的首頁頻道。

/cron add "0 9 * * 1" "Generate a weekly report covering:

1. Search the web for the top 5 AI news stories from the past week
2. Search GitHub for trending repositories in the 'machine-learning' topic
3. Check Hacker News for the most discussed AI/ML posts

Format as a clean summary with sections for each source. Include links.
Keep it under 500 words — highlight only what matters." --name "Weekly AI digest" --deliver telegram

從 CLI 執行:

hermes cron create "0 9 * * 1" \
  "Generate a weekly report covering the top AI news, trending ML GitHub repos, and most-discussed HN posts. Format with sections, include links, keep under 500 words." \
  --name "Weekly AI digest" \
  --deliver telegram

0 9 * * 1 是標準的 Cron 表達式:每週一早上 9:00。


模式三:GitHub 儲存庫監視器

監控儲存庫的新 Issue、PR 或版本發布。

/cron add "every 6h" "Check the GitHub repository NousResearch/hermes-agent for:
- New issues opened in the last 6 hours
- New PRs opened or merged in the last 6 hours
- Any new releases

Use the terminal to run gh commands:
  gh issue list --repo NousResearch/hermes-agent --state open --json number,title,author,createdAt --limit 10
  gh pr list --repo NousResearch/hermes-agent --state all --json number,title,author,createdAt,mergedAt --limit 10

Filter to only items from the last 6 hours. If nothing new, respond with [SILENT].
Otherwise, provide a concise summary of the activity." --name "Repo watcher" --deliver discord

警告——自含式提示

注意提示中如何包含確切的 gh 指令。Cron 代理人不會記住之前的執行結果或你的偏好——請清楚說明所有細節。


模式四:資料收集管線

定期抓取資料、儲存到檔案,並隨時間偵測趨勢。這個模式結合了腳本(用於收集)和代理人(用於分析)。

import json, os, urllib.request
from datetime import datetime

DATA_DIR = os.path.expanduser("~/.hermes/data/prices")
os.makedirs(DATA_DIR, exist_ok=True)

# Fetch current data (example: crypto prices)
url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd"
data = json.loads(urllib.request.urlopen(url, timeout=30).read())

# Append to history file
entry = {"timestamp": datetime.now().isoformat(), "prices": data}
history_file = os.path.join(DATA_DIR, "history.jsonl")
with open(history_file, "a") as f:
    f.write(json.dumps(entry) + "\n")

# Load recent history for analysis
lines = open(history_file).readlines()
recent = [json.loads(l) for l in lines[-24:]]  # Last 24 data points

# Output for the agent
print(f"Current: BTC=${data['bitcoin']['usd']}, ETH=${data['ethereum']['usd']}")
print(f"Data points collected: {len(lines)} total, showing last {len(recent)}")
print(f"\nRecent history:")
for r in recent[-6:]:
    print(f"  {r['timestamp']}: BTC=${r['prices']['bitcoin']['usd']}, ETH=${r['prices']['ethereum']['usd']}")
/cron add "every 1h" "Analyze the price data from the script output. Report:
1. Current prices
2. Trend direction over the last 6 data points (up/down/flat)
3. Any notable movements (>5% change)

If prices are flat and nothing notable, respond with [SILENT].
If there's a significant move, explain what happened." \
  --script ~/.hermes/scripts/collect-prices.py \
  --name "Price tracker" \
  --deliver telegram

腳本負責機械性的收集工作;代理人則加入推理層。


模式五:多技能工作流程

將技能串聯起來處理複雜的排程任務。技能會按順序載入,然後提示才會執行。

# Use the arxiv skill to find papers, then the obsidian skill to save notes
/cron add "0 8 * * *" "Search arXiv for the 3 most interesting papers on 'language model reasoning' from the past day. For each paper, create an Obsidian note with the title, authors, abstract summary, and key contribution." \
  --skill arxiv \
  --skill obsidian \
  --name "Paper digest"

直接從工具呼叫:

cronjob(
    action="create",
    skills=["arxiv", "obsidian"],
    prompt="Search arXiv for papers on 'language model reasoning' from the past day. Save the top 3 as Obsidian notes.",
    schedule="0 8 * * *",
    name="Paper digest",
    deliver="local"
)

技能按順序載入——先 arxiv(教代理人如何搜尋論文),然後 obsidian(教如何撰寫筆記)。提示將兩者串聯在一起。


管理你的工作

# List all active jobs
/cron list

# Trigger a job immediately (for testing)
/cron run <job_id>

# Pause a job without deleting it
/cron pause <job_id>

# Edit a running job's schedule or prompt
/cron edit <job_id> --schedule "every 4h"
/cron edit <job_id> --prompt "Updated task description"

# Add or remove skills from an existing job
/cron edit <job_id> --skill arxiv --skill obsidian
/cron edit <job_id> --clear-skills

# Remove a job permanently
/cron remove <job_id>

傳遞目標

--deliver 選項控制結果的傳送位置:

目標範例使用情境
origin--deliver origin建立工作的同一個對話(預設)
local--deliver local僅儲存到本地檔案
telegram--deliver telegram你的 Telegram 首頁頻道
discord--deliver discord你的 Discord 首頁頻道
slack--deliver slack你的 Slack 首頁頻道
特定聊天--deliver telegram:-1001234567890特定的 Telegram 群組
主題串--deliver telegram:-1001234567890:17585特定的 Telegram 主題串

小技巧

讓提示自含。 Cron 工作中的代理人不會記住你的對話。請直接在提示中包含網址、儲存庫名稱、格式偏好和傳遞指示。

有意識地使用 [SILENT] 對於監控工作,請加入類似「如果沒有變更,只回覆 [SILENT]」的指示。不要要求代理人解釋靜默情況下的標記——Cron 會將 [SILENT] 視為傳遞抑制標記。

用腳本收集資料。 script 參數讓 Python 腳本處理無趣的部分(HTTP 請求、檔案 I/O、狀態追蹤)。代理人只看到腳本的 stdout 並對其進行推理。這比讓代理人自己抓取資料更便宜且更可靠。

/cron run 測試。 在等待排程觸發之前,使用 /cron run <job_id> 立即執行並驗證輸出是否正確。

排程表達式。 支援的格式:相對延遲(30m)、間隔(every 2h)、標準 Cron 表達式(0 9 * * *)和 ISO 時間戳記(2025-06-15T09:00:00)。不支援自然語言如 daily at 9am——請改用 0 9 * * *


完整的 Cron 參考——所有參數、邊界情況和內部機制——請見排程任務(Cron)



使用技能