Hermes Agent: 定时任务

最后更新:2026-08-31

定时任务让 Hermes Agent 变成你的自动值班助手——每天早上自动汇总新闻,每周五自动生成周报,无需你手动触发。

💡 提示:Hermes 的 Cron 系统是内置的,不需要外部调度器(如 crontab)。任务定义直接写在配置中,与技能、记忆深度集成。

📋 前置知识:第7课 Skills 技能系统

1. 你将学到

编号 内容
Cron 调度语法
任务创建与配置
定时触发技能
任务监控与日志
异常处理与重试

2. 故事

(1) 痛点:每天重复操作

Bob 每天早上要花 15 分钟:查邮件、看 Git 状态、浏览技术新闻。每周五还要手动写周报。

(2) 解法:Agent 自动值班

Alice 设置了定时任务,Agent 自动完成:

BASH
# 每天早上 9:00 自动执行
⏰ 09:00 Agent: 早上好!今日摘要:
   - 3 封未读邮件(1封紧急)
   - 2 个 PR 待审查
   - 技术热点:React 19 正式发布

# 每周五 17:00 自动生成周报
⏰ Fri 17:00 Agent: 本周周报已生成:
   - 完成 12 个 commit
   - 修复 5 个 bug
   - 2 个 PR 已合并
   [周报已发送到你的邮箱]

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/Shanghai"
  
  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 空闲时几乎为零。执行时取决于任务内容,通常 <1% CPU。
Q 如何调试定时任务?
A hermes cron run <name> 手动触发查看完整日志,不需要等待定时。

📖 小节


📝 作业

  1. 基础题(难度⭐):创建一个每天早上 9 点执行的定时任务,发送 "Good Morning" 到 Telegram。
  2. 进阶题(难度⭐⭐):创建一个结合技能和工具的定时任务,每天自动汇总 Git 提交记录并发邮件。
  3. 挑战题(难度⭐⭐⭐):设计一套完整的自动化日程系统(晨报+午间提醒+晚间总结+周报),包含异常处理和降级策略。
Web-Tutorial.com

Web-Tutorial 技术团队

由多位开发者共同维护的编程教程平台。每篇教程由对应领域的开发者编写和审核,确保内容准确可靠。如发现任何问题,欢迎向我们反馈。

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏