ByteNoteByteNote

字节笔记本

2026年6月21日

hermes教程-使用Cron自动化一切

API中转
¥120

每日简报机器人教程涵盖了基础知识。本指南更进一步——提供五种真实世界的自动化模式,你可以根据自己的工作流程进行调整。

完整功能参考,请参见计划任务(Cron)

信息——关键概念

Cron 任务在全新的智能体会话中运行,不会记忆你当前聊天的内容。提示必须完全自包含——包含智能体需要知道的一切。

提示——不需要 LLM?你有两种零令牌选项。

  • 周期性看门狗,脚本已经生成确切消息(内存告警、磁盘告警、心跳):使用仅脚本 cron 任务。相同的调度器,无需 LLM。你可以在聊天中让 Hermes 设置一个——cronjob 工具知道何时选择 no_agent=True 并为你编写脚本。
  • 从已在运行的脚本一次性触发(CI 步骤、提交后钩子、部署脚本、外部调度的监控器):使用 hermes send 将标准输出或文件直接通过管道发送到 Telegram / Discord / Slack / 等,无需设置 cron 条目。

模式1:网站变更监控

监控一个 URL 的变化,只有在有变化时才收到通知。

script 参数是秘密武器。一个 Python 脚本在每次执行前运行,其标准输出成为智能体的上下文。脚本处理机械工作(抓取、差异比较);智能体处理推理(这个变化有趣吗?)。

创建监控脚本:

bash
mkdir -p ~/.hermes/scripts
python
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 任务:

bash
/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] 视为静默标记,因此你只会在实际发生事件时收到通知——安静时段不会收到垃圾信息。


模式2:周报

从多个来源编译信息,生成格式化的摘要。每周运行一次,并发送到你的主频道。

bash
/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:

bash
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。


模式3:GitHub 仓库监控

监控仓库的新 issue、PR 或发布。

bash
/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 智能体没有之前运行或你偏好的记忆——请明确说明一切。


模式4:数据收集管道

按固定间隔抓取数据,保存到文件,并检测随时间变化的趋势。此模式结合了脚本(用于收集)和智能体(用于分析)。

python
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']}")
bash
/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

脚本处理机械收集;智能体添加推理层。


模式5:多技能工作流

将多个技能串联起来,完成复杂的计划任务。技能按顺序加载,然后执行提示。

bash
## 使用 arxiv 技能查找论文,然后使用 obsidian 技能保存笔记
/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"

直接从工具使用:

python
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(教如何写笔记)。提示将它们串联起来。


管理你的任务

bash
## 列出所有活跃任务
/cron list
## 立即触发一个任务(用于测试)
/cron run <job_id>
## 暂停一个任务而不删除
/cron pause <job_id>
## 编辑运行中任务的调度或提示
/cron edit <job_id> --schedule "every 4h"
/cron edit <job_id> --prompt "Updated task description"
## 向现有任务添加或移除技能
/cron edit <job_id> --skill arxiv --skill obsidian
/cron edit <job_id> --clear-skills
## 永久删除一个任务
/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 任务中的智能体没有你对话的记忆。直接在提示中包含 URL、仓库名称、格式偏好和交付说明。

有意识地使用 [SILENT] 对于监控任务,包含类似“如果无变化,仅回复 [SILENT]”的指令。不要在静默情况下要求智能体解释该标记——cron 将 [SILENT] 视为交付抑制标记。

使用脚本进行数据收集。 script 参数让 Python 脚本处理枯燥的部分(HTTP 请求、文件 I/O、状态跟踪)。智能体只看到脚本的标准输出,并对其应用推理。这比让智能体自己抓取更便宜、更可靠。

使用 /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)


分享: