🤖HermesBlog
Hermes 官方教程 · 第21篇2026/8/9· Easy Understand Hermes Agent

定时任务(Cron)——让 AI 自动干活

Hermes Agent 官方教程第21篇:让 AI 自动干活

定时任务:给 AI 装上闹钟

定时任务(Cron)——让 AI 自动干活

上次我们聊了每日简报机器人,今天来点更硬核的——五个真实世界的自动化套路,直接抄作业就能用。

⚠️ 先记住一个关键点

Cron 任务跑在全新的会话里,AI 完全不记得你当前聊天的上下文。所以你的提示词必须自包含——把 AI 需要知道的一切都写进去,别指望它“记得”。

🎁 两个不用花 token 的省钱方案

  • 脚本自己就能出结果(比如磁盘告警、心跳检测):用纯脚本 cron,不调 LLM。直接在聊天里让 Hermes 帮你建,cronjob 工具会自动判断什么时候该用 no_agent=True 并帮你写好脚本。
  • 脚本已经在跑了(CI 步骤、部署钩子):用 hermes send 把 stdout 或文件直接推到 Telegram / Discord / Slack,根本不用建 cron。

套路一:网站改版监控

只在你关心的页面发生变化时才通知你。秘密武器是 script 参数——Python 脚本先跑一遍,它的 stdout 变成 AI 的上下文。脚本干机械活(抓取、比对),AI 干思考活(这变化值不值得关注)。

先建监控脚本:

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)

# 输出给 AI 看
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] 小技巧:监控类任务让 AI 没事就回 [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

命令行版:

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 点


套路三: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 里的偏好,但别把关键细节寄托在上面。)


套路四:数据采集管道

定时抓数据存文件,让 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"Collected prices at {entry['timestamp']}")
print(f"History file: {history_file}")
print(f"Latest data: {json.dumps(data)}")

配合 cron 定时跑,AI 每次读取历史文件,帮你发现趋势变化。


这五个套路覆盖了监控、报告、追踪、采集四大场景。核心就一句话:脚本做笨活,AI 做聪明活,提示词写完整。拿去改造你自己的 workflow 吧!

📖 官方文档

本文根据 Hermes Agent 官方文档编写,原文见:官方文档 › guides/automate-with-cron