定时任务(Cron)——让 AI 自动干活
Hermes Agent 官方教程第21篇:让 AI 自动干活
定時任務(Cron)——讓 AI 自動幹活
上一期我們聊了每日簡報機器人,今天來點更硬核的——五個真實世界的自動化套路,讓你的 Hermes 從「聊天助手」升級成「全能打工人」。
先記住一個關鍵概念
Cron 任務每次運行都是全新的會話,它不記得你剛才聊了什麼。所以你的提示詞必須完全自包含——把 AI 需要知道的一切都寫進去,別指望它「猜」你的意圖。
另外,如果你不需要 AI 動腦子(比如只是發個磁碟告警、心跳檢測),還有兩個零 token 消耗的選擇:
- 純腳本定時任務:腳本直接產出最終訊息,Hermes 在聊天裡就能幫你設定(
cronjob工具會自動判斷什麼時候用no_agent=True) - 一次性腳本輸出:用
hermes send把 stdout 或檔案內容直接推到 Telegram / Discord / Slack,連定時都不用設
套路一:網站變更監控
想知道某個網頁什麼時候更新了?讓腳本幹苦力,AI 做判斷。
先寫個監控腳本:
import hashlib, json, os, urllib.request
URL = "https://example.com/pricing"
STATE_FILE = os.path.expanduser("~/.hermes/scripts/.watch-site-state.json")
# 抓取當前內容
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()
# 讀取上次的雜湊
prev_hash = None
if os.path.exists(STATE_FILE):
with open(STATE_FILE) as f:
prev_hash = json.load(f).get("hash")
# 儲存當前狀態
with open(STATE_FILE, "w") as f:
json.dump({"hash": current_hash, "url": URL}, f)
# 輸出給 AI 看
if prev_hash and prev_hash != current_hash:
print(f"CHANGE DETECTED on {URL}")
print(f"Current content (first 2000 chars):\n{content[:2000]}")
else:
print("NO_CHANGE")
然後設定定時任務:
/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] 小技巧:讓 AI 在沒變化時只回覆 [SILENT],Cron 就會安靜地不打擾你——只有真正有變化時才通知,絕不洗版。
套路二:每週自動報告
每週一早上 9 點,AI 自動蒐集資訊、整理成報告發給你:
/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
0 9 * * 1 是標準 cron 表達式:每週一上午 9 點。
套路三:GitHub 倉庫監控
盯著某個倉庫的新 issue、PR 和 release:
/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 代理沒有上次對話的記憶——雖然持久記憶(MEMORY.md)會載入,但別把關鍵細節寄託在它身上。
套路四:資料採集管道
定時抓資料存檔,讓 AI 分析趨勢。腳本負責採集,AI 負責洞察:
import json, os, urllib.request
from datetime import datetime
DATA_DIR = os.path.expanduser("~/.hermes/data/prices")
os.makedirs(DATA_DIR, exist_ok=True)
# 抓取當前資料(範例:加密貨幣價格)
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())
# 追加到歷史檔案
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")
# 輸出給 AI 分析
print(f"Latest prices: {data}")
print(f"History file: {history_file}")
配合 /cron add "every 1h" "分析價格趨勢..." --script ~/.hermes/scripts/collect-prices.py,就能實現定時採集 + AI 分析趨勢的完整管道。
套路五:定時提醒與待辦
這個最簡單——直接讓 AI 當你的生活管家:
/cron add "0 9 * * *" "Check my calendar and remind me of today's meetings. Also list any overdue tasks from my todo list." --name "Morning briefing" --deliver telegram
每天早上 9 點,Hermes 自動幫你梳理一天的安排。
總結
這四個套路覆蓋了監控、報告、追蹤、採集四大類自動化場景。核心心法就兩條:
- 腳本幹機械活,AI 幹思考活
- 提示詞必須自包含,別讓 AI 猜
去試試吧,讓 Hermes 真正成為你的 24/7 全能打工人!
📖 官方文档
本文根据 Hermes Agent 官方文档编写,原文见:官方文档 › guides/automate-with-cron