Hermes Agent: 定期タスク

最終更新:2026-08-31

定期タスクはHermes Agentを自動当番アシスタントに変えます。毎朝自動でニュースダイジェスト、毎週金曜に週報を自動生成、手動トリガー不要です。

💡 ヒント: HermesのCronシステムは内蔵されており、外部スケジューラ(crontabなど)は不要です。タスク定義は設定に直接書き込み、スキルやメモリと深く統合されています。

📋 前提知識: 第7課 スキルシステム

1. 学ぶ内容

# 内容
Cronスケジュール構文
タスクの作成と設定
定期スキルトリガー
タスク監視とログ
例外処理とリトライ

2. ストーリー

(1) 課題:毎日同じ操作の繰り返し

Bobは毎朝15分かけて:メールチェック、Gitステータス確認、技術ニュース閲覧。毎週金曜には手動で週報を書く必要もある。

(2) 解決策:エージェントが自動当番

Aliceは定期タスクを設定し、エージェントが自動実行:

BASH
# 毎日午前9:00に自動実行
⏰ 09:00 Agent: おはよう!今日のダイジェスト:
   - 未読メール3通(緊急1通)
   - レビュー待ちPR 2件
   - 技術トピック:React 19正式リリース

# 毎週金曜17:00に週報自動生成
⏰ Fri 17:00 Agent: 今週の週報を生成しました:
   - 12コミット完了
   - 5バグ修正
   - 2PRマージ済み
   [週報をメールに送信しました]

3. Cronスケジュール構文

(1) 基本構文

┌───────────── 分 (0-59)
│ ┌───────────── 時 (0-23)
│ │ ┌───────────── 日 (1-31)
│ │ │ ┌───────────── 月 (1-12)
│ │ │ │ ┌───────────── 曜日 (0-6, 0=日曜)
│ │ │ │ │
* * * * *

(2) よく使う表現式

表現式 意味
0 9 * * * 毎日午前9:00
0 9 * * 1-5 平日9:00
0 17 * * 5 毎週金曜17:00
*/30 * * * * 30分ごと
0 9,12,18 * * * 毎日9:00、12:00、18:00
0 0 1 * * 毎月1日0:00

4. タスクの作成と設定

(1) 設定ファイル方式

YAML
# ~/.hermes/config.yaml
cron:
  enabled: true
  timezone: "Asia/Tokyo"
  
  tasks:
    # デイリー簡報
    - name: "daily-briefing"
      schedule: "0 9 * * 1-5"
      description: "平日朝のダイジェスト"
      skill: "daily-briefing"
      notify:
        platform: "telegram"
        chat_id: 123456789
      
    # 週次週報
    - name: "weekly-report"
      schedule: "0 17 * * 5"
      description: "金曜の週報生成"
      actions:
        - type: "tool"
          name: "code_shell"
          command: "git log --since='1 week ago' --oneline"
        - type: "skill"
          name: "report-generator"
        - type: "tool"
          name: "email_send"
          to: "alice@company.com"
          subject: "今週の作業週報"
      
    # コード品質チェック
    - name: "code-quality-check"
      schedule: "0 2 * * *"     # 毎日午前2時
      actions:
        - type: "tool"
          name: "code_shell"
          command: "cd ~/projects/myapp && npm run lint"
        - type: "tool"
          name: "code_shell"
          command: "cd ~/projects/myapp && npm test"
      on_failure:
        notify: "telegram"
        message: "⚠️ コード品質チェックが不合格"

(2) コマンドライン方式

BASH
# 定期タスクの作成
hermes cron add \
  --name "morning-briefing" \
  --schedule "0 9 * * 1-5" \
  --skill "daily-briefing" \
  --notify telegram

# 全タスク一覧
hermes cron list

# 手動トリガー(スケジュールを待たずに実行)
hermes cron run morning-briefing

# 一時停止/再開
hermes cron pause morning-briefing
hermes cron resume morning-briefing

# タスクの削除
hermes cron delete morning-briefing

(3) 会話中の作成

BASH
me: 毎日朝9時に技術ニュースダイジェストを送って
Agent: 定期タスクを作成します:
  タスク名: tech-news-digest
  スケジュール: 0 9 * * *
  アクション: web_search → summarize → notify
  
  作成しますか?(y/n)
me: y
Agent: ✅ 定期タスク "tech-news-digest" を作成しました!

5. 定期スキルトリガー

(1) スキル+Cronの組み合わせ

YAML
# スキルファイル:daily-briefing.yaml
name: "daily-briefing"
description: "毎日の作業ダイジェスト"
type: "cron-triggered"

actions:
  - type: "tool"
    name: "email_check"
    params:
      folder: "inbox"
      unread_only: true
      
  - type: "tool"
    name: "code_shell"
    command: "git -C ~/projects log --since='1 day ago' --oneline"
    
  - type: "tool"
    name: "web_search"
    query: "技術ニュース AI 開発"
    
  - type: "generate"
    template: |
      📋 今日のダイジェスト({{ date }})
      
      📧 未読メール: {{ email_count }} 通
      🔨 Git Commits: {{ commit_count }} 件
      📰 技術トピック:
      {{#each news}}
      - {{this.title}}
      {{/each}}

(2) 条件付き実行

YAML
cron:
  tasks:
    - name: "pr-review-reminder"
      schedule: "0 10 * * 1-5"
      condition: "pending_prs > 0"   # レビュー待ちPRがある時のみ実行
      actions:
        - type: "tool"
          name: "github_api"
          endpoint: "/repos/{owner}/{repo}/pulls"
          params:
            state: "open"
        - type: "notify"
          platform: "slack"
          message: "{{count}}件のPRがレビュー待ちです"

6. タスク監視とログ

(1) 実行履歴の確認

BASH
# 全タスクの実行記録を確認
hermes cron history

# 出力例:
# ┌──────────────────┬──────────┬─────────┬────────┬──────────┐
# │ Task             │ Schedule │ Status  │ Duration│ Last Run │
# ├──────────────────┼──────────┼─────────┼────────┼──────────┤
# │ daily-briefing   │ 0 9 * *  │ ✅ OK   │ 12s    │ 09:00    │
# │ weekly-report    │ 0 17 * 5 │ ✅ OK   │ 45s    │ Fri 17:00│
# │ code-quality     │ 0 2 * *  │ ❌ FAIL │ 3s     │ 02:00    │
# └──────────────────┴──────────┴─────────┴────────┴──────────┘

(2) 実行ログ

BASH
# 個別タスクの詳細ログ
hermes cron logs daily-briefing --last

# 出力例:
# [09:00:01] Task "daily-briefing" started
# [09:00:02] → tool: email_check → 3 unread
# [09:00:05] → tool: code_shell → 5 commits
# [09:00:08] → tool: web_search → 4 results
# [09:00:10] → generate: summary
# [09:00:12] → notify: telegram → sent
# [09:00:12] Task "daily-briefing" completed (12s)

7. 例外処理とリトライ

(1) リトライ戦略

YAML
cron:
  tasks:
    - name: "api-health-check"
      schedule: "*/5 * * * *"
      actions:
        - type: "tool"
          name: "web_api"
          url: "https://api.example.com/health"
      
      # リトライ戦略
      retry:
        max_attempts: 3
        backoff: "exponential"     # exponential / linear / fixed
        initial_delay: 5           # 最初のリトライまで5秒
        max_delay: 60              # 最大待機60秒
        
      # 失敗通知
      on_failure:
        notify: "telegram"
        message: "⚠️ APIヘルスチェック失敗、3回リトライ済み"
        escalate_after: 3          # 3回連続失敗後にエスカレーション通知

(2) タイムアウトとデグラデーション

YAML
cron:
  tasks:
    - name: "daily-briefing"
      schedule: "0 9 * * 1-5"
      timeout: 120                 # 120秒でタイムアウト
      on_timeout:
        action: "notify"
        message: "⚠️ 朝のダイジェスト生成がタイムアウト、簡易版に切り替え"
        fallback_skill: "daily-briefing-simple"

❓ よくある質問

Q 定期タスクは正確ですか?
A 分単位の精度です。システム負荷による数秒の偏差の可能性あり、サブ秒精度の用途には不向きです。
Q シャットダウン中の定期タスクはどうなりますか?
A シャットダウン中のタスクはスキップされます。run_on_startup: trueを設定すれば、起動後に逃したタスクを補完実行できます。
Q 定期的にスキルやツールをトリガーできますか?
A はい。Cronはスキル、ツールチェーン、Shellコマンド、API呼び出しをトリガーできます。
Q タイムゾーンの設定方法は?
A cron.timezone設定項目、デフォルトはシステムタイムゾーンに従います。全IANAタイムゾーン名に対応。
Q 定期タスクのリソース消費は?
A アイドル時はほぼゼロ。実行時はタスク内容に依存、通常CPU使用率1%未満。
Q 定期タスクのデバッグ方法は?
A hermes cron run <name>で手動トリガーして完全なログを確認、スケジュールを待つ必要はありません。

📖 まとめ


📝 練習問題

  1. 基本(⭐): 毎日午前9時に実行され、"Good Morning"をTelegramに送信する定期タスクを作成してください。
  2. 中級(⭐⭐): スキルとツールを組み合わせた定期タスクを作成し、毎日Gitコミット記録を自動集計してメール送信してください。
  3. 上級(⭐⭐⭐): 完全な自動化スケジュールシステム(朝のダイジェスト+昼のリマインダー+夜のまとめ+週報)を設計し、例外処理とデグラデーション戦略を含めてください。
Web-Tutorial.com

Web-Tutorial 技術チーム

複数の開発者によって共同維持されているプログラミングチュートリアルプラットフォーム。各チュートリアルは専門分野の開発者が執筆・レビューしています。正確で信頼性の高いコンテンツを目指しています — 問題を見つけた場合はお知らせください。

100%