クロンをもっと使いこなす:今日から使える5つの自動化パターン
Cron Troubleshooting Guide — easy-to-understand guide based on official docs
クロンをもっと使いこなす:今日から使える5つの自動化パターン
Hermes Agentで最初のcronジョブを設定して、もう夢中になっていることでしょう。でも、実はもっとたくさんのことができるのをご存知ですか?デイリーブリーフィングボットのチュートリアルはほんの入り口にすぎません。ここでは、あなたの生活をもっと楽にする5つの実践的な自動化パターンを紹介します。
黄金のルール:自己完結型プロンプト
本題に入る前に、最も重要なことをお伝えします:cronジョブは新しいエージェントセッションで実行されます。エージェントはあなたの現在のチャットの記憶を一切持っていません。つまり、すべてのプロンプトは完全に自己完結型である必要があります——コンテキストから正確なコマンドまで、エージェントが必要とするすべての情報を含めてください。
LLMが不要なときはスキップする
すべての自動化に言語モデルが必要なわけではありません。トークンを消費しない2つのオプションがあります:
- 定期ウォッチドッグ:スクリプトがすでに正確なメッセージを生成している場合(メモリアラートやハートビートなど)、スクリプト専用cronジョブを使用します。チャットでHermesに頼むだけで、
cronjobツールがno_agent=Trueを使うべきタイミングを判断し、スクリプトを自動生成してくれます。 - 実行中スクリプトからのワンショット:CIステップやデプロイフックには、
hermes sendを使って出力をそのままTelegramやDiscordにパイプできます。cronの設定は一切不要です。
パターン1:ウェブサイト変更モニター
価格ページが変わったときや、競合他社がサイトを更新したときに知りたいと思いませんか?このパターンでは、Pythonスクリプトが機械的な作業を行い、エージェントが推論を担当します。
まず、監視スクリプトを作成します:
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")
# 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
プロのコツ:[SILENT]テクニックは本当に便利です。何も変更がないとき、エージェントは[SILENT]とだけ応答し、cron配信はそれを静音マーカーとして扱います。実際に何かが起きたときだけ通知が届くので、静かな時間帯にスパムが届くことはありません。
パターン2:週次レポート
複数のソースから情報を収集し、整形されたサマリーにまとめます。これは毎週月曜日の午前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
CLIからは、次のようになります:
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時を意味します。
パターン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
注意:プロンプトに正確なghコマンドが含まれていることに気づきましたか?それは意図的です。cronエージェントには会話履歴がありません——すべてを明示的に記述してください。(永続メモリは読み込まれるので、MEMORY.mdに保存された長期的な設定は引き継がれますが、ジョブの重要な詳細をそれに依存しないでください。)
パターン4:データ収集パイプライン
定期的にデータをスクレイピングし、ファイルに保存して、時間の経過に伴うトレンドを検出します。これは、収集用スクリプトと分析用エージェントを組み合わせたパターンです:
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")
print(f"Collected prices at {entry['timestamp']}: BTC ${data['bitcoin']['usd']}, ETH ${data['ethereum']['usd']}")
次に、スクリプトを実行し、収集したデータのトレンドをエージェントに分析させるcronジョブを設定します。
パターン5:スクリプト専用ウォッチドッグ
スクリプトがすでに正確なメッセージを生成するアラートの場合は、LLMを完全にスキップします:
/cron add "every 5m" --script ~/.hermes/scripts/check-disk.py --no-agent --deliver slack
cronjobツールは、適切な場合に自動的にno_agent=Trueを選択し、スクリプトも自動生成してくれます。
自動化の準備はできましたか?
これらのパターンは出発点にすぎません。自由に組み合わせてください——機械的な作業にはスクリプトを、推論にはエージェントを、そして[SILENT]で通知をクリーンに保ちましょう。全機能のリファレンスは、スケジュールタスク(Cron)のドキュメントをご覧ください。自動化を楽しんでください!
📖 公式ドキュメント
この記事は Hermes Agent の公式ドキュメントに基づいています:公式ドキュメント › guides/automate-with-cron