实战:纯脚本定时任务
Hermes 实战教程第16篇:纯脚本定时任务。不用 AI 也能定时跑。
实战:纯脚本定时任务
上一期我们聊了每日简报机器人,今天来点更硬核的——纯脚本定时任务。啥意思?就是有些监控活儿根本不用麻烦大模型,脚本自己就能搞定,省 token 又省心。
先搞懂核心概念
定时任务跑在全新的会话里,它不记得你之前聊过啥。所以你的提示词必须自包含——把 agent 需要知道的一切都写进去。
另外,如果你不需要 LLM 参与,有两个零 token 的选择:
- 循环看门狗:脚本已经能产出最终消息(比如内存告警、磁盘告警),用
no_agent=True的纯脚本定时任务就行。直接在聊天里让 Hermes 帮你建,cronjob工具会自动判断并写好脚本。 - 一次性脚本:如果你在 CI、post-commit 钩子或部署脚本里,直接用
hermes send把 stdout 或文件内容推到 Telegram / Discord,连定时任务都不用建。
模式一:网站变更监控
script 参数是秘密武器。Python 脚本每次执行前跑一遍,stdout 变成 agent 的上下文。脚本干机械活(抓取、比对),agent 干思考活(这变化有意思吗?)。
先建监控脚本:
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")
# 抓取当前内容
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)
# 输出给 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 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] 小技巧:监控类任务让 agent 没事就回 [SILENT],cron 投递会把它当静默标记,你只在真正有事时才收到通知,安静时段不打扰。
模式二:每周报告
每周一早上 9 点自动汇总各路信息:
/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 agent 没有上次的对话记录,一切都要写清楚。(持久记忆 MEMORY.md 会加载,但别把关键细节押在它上面。)
模式四:数据采集管道
定时抓数据存文件,让 agent 分析趋势。脚本负责采集,agent 负责分析,完美分工。脚本写法类似模式一,把数据追加到 history.jsonl 文件里,然后让 agent 读取分析。
总结:纯脚本定时任务的核心思路就是——能用脚本干的别麻烦 LLM,省 token 又稳定。需要动脑子的才交给 agent。试试看,你会爱上这种省心又高效的感觉!
📖 官方文档
本文根据 Hermes Agent 官方文档编写,原文见:官方文档 › guides/automate-with-cron