cron で何でも自動化する
目次
基本は毎日のブリーフィングボットのチュートリアルで扱っています。このガイドはその先の話です。自分の作業に合わせて応用できる、実務的な自動化のパターンを 5 つ紹介します。
機能の全体像は定期実行タスク(cron)を参照してください。
パターン 1: Web ページの変更監視
URL を見張り、何かが変わったときにだけ知らせを受け取ります。
ここで効いてくるのが script のパラメータです。実行のたびに事前に Python のスクリプトが走り、その標準出力がエージェントへのコンテキストになります。取得や差分の検出といった機械的な作業はスクリプトが受け持ち、「この変更は注目に値するか」という判断はエージェントが受け持ちます。
監視用のスクリプトを作ります。
mkdir -p ~/.hermes/scripts
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パターン 2: 週次のレポート
複数の情報源をまとめて、体裁の整った要約にします。これは週に 1 回動いて、自分のホームのチャンネルに届きます。
/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 telegramCLI からは次のようにします。
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 telegram0 9 * * 1 は標準的な cron の書式で、毎週月曜の午前 9 時を意味します。
パターン 3: 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パターン 4: データ収集のパイプライン
一定の間隔でデータを集めてファイルに保存し、時間の経過による傾向を捉えます。このパターンは、収集を担うスクリプトと分析を担うエージェントを組み合わせます。
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機械的な収集はスクリプトが担い、その上に判断の層をエージェントが足します。
パターン 5: 複数のスキルを組み合わせる
込み入った定期タスクでは、スキルを数珠つなぎにします。スキルはプロンプトの実行前に、指定した順で読み込まれます。
# 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(メモの書き方を教える)です。その 2 つをつなぐのがプロンプトの役目です。
ジョブを管理する
# 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 の特定のトピックのスレッド |
| Bot Chat | --deliver bot-chat |
このプロファイルの正となる Bot Chat に出力を流し込みます。ボットがそれを読んで応答します |
| Bot Chat(名前を指定) | --deliver bot-chat:research |
同じ端末にある別のプロファイルの Bot Chat |
Bot Chat への配信
bot-chat を指定すると、ジョブの出力はプロファイルの正となる「Bot Chat」のセッションへ実際のメッセージとして届きます。ボットはそれを他の メッセージと同じように受け取り、対応が必要なものには対応し、そのチャットで 返事をします。定期実行の出力を実行履歴に残すだけでなく、ボットに*見せて 反応させたい*ときはこの配信先を使います。
知っておきたい点は次のとおりです。
- その端末の中だけで完結します。 指定するプロファイルは、スケジューラを
動かしている端末に存在している必要があります(hermes profile list)。名前は 作成時に検証され、別のゲートウェイや別の端末のプロファイルは指定できません。
- ボットのターンを 1 回消費します。 配信のたびに、届け先のボットの Bot Chat で
エージェントのターンが 1 回まるごと動きます。頻度の高いジョブでは費用を見込んでおいてください。
- 組み合わせられます。
--deliver bot-chat,telegramとすれば、ボットと自分の
Telegram のホームチャンネルの両方に届きます。all という指定が bot-chat を含むことはありません。
- 届いたメッセージには印が付くので、ボットは利用者からではなく定期実行の
ジョブから来たものだと分かります。
コツ
プロンプトはそれだけで完結させる。 cron のジョブの中のエージェントは、これまでの会話を覚えていません。URL、リポジトリ名、体裁の好み、配信の指示は、すべてプロンプトに直接書いてください。
[SILENT] を意識して使う。 監視のジョブでは「何も変わっていなければ [SILENT] とだけ答えること」のように指示します。静かな場合にこの印を説明させてはいけません。cron は [SILENT] を配信を止めるための印として扱います。
データ収集にはスクリプトを使う。 script のパラメータを使えば、HTTP のリクエスト、ファイルの読み書き、状態の記録といった退屈な部分を Python のスクリプトに任せられます。エージェントが見るのはスクリプトの標準出力だけで、そこに判断を加えます。取得までエージェントにやらせるより安く、確実です。
/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)を参照してください。*