返回

Hermes Agent 教程 8:cron 调度 — 自动化与定时报告

使用 Hermes Agent 内置的 cron 调度器创建自动化任务。本文介绍自然语言调度、平台投递、任务类型,以及计划管理。

教程概述

系列索引: Hermes Agent 教程系列

何时需要读这篇:如果你需要无人值守地运行自动化任务、日报、夜间备份或每周审计,那么这篇适合你。

本文介绍 Hermes Agent 的cron 调度能力,也就是内置的周期性任务自动化系统。

你将学到什么

  • ✅ Hermes 中的 cron 架构
  • ✅ 如何使用自然语言创建定时任务
  • ✅ 如何把结果投递到消息平台
  • ✅ 常见任务类型与示例
  • ✅ 如何管理和监控计划任务

cron 架构

Hermes 的 cron 如何工作

flowchart TD
    A[cron 定义] --> B[调度守护进程]
    B --> C{到触发时间了吗?}
    C -->|是| D[拉起 Hermes 任务]
    C -->|否| E[继续等待]
    E --> C
    D --> F[执行任务]
    F --> G[生成输出]
    G --> H[投递到平台]

    style B fill:#e1f5ff
    style H fill:#fff3e0

组成部分

组件 用途
调度守护进程 后台监控触发条件
任务拉起器 为每个任务创建独立 Hermes 实例
输出处理器 格式化并投递结果
平台投递层 发送到 Telegram、Discord 等平台

创建定时任务

自然语言调度

Hermes 支持使用自然语言定义计划:

# 每天上午 9 点生成日报
hermes cron add "every day at 9am" --task "generate daily report"

# 每周五下午 5 点生成周报
hermes cron add "every friday at 5pm" --task "weekly summary"

# 每小时执行健康检查
hermes cron add "every hour" --task "check system status"

标准 cron 格式

同样支持传统 cron 表达式:

hermes cron add "0 9 * * *" --task "daily report"
hermes cron add "0 17 * * 5" --task "friday summary"

任务定义

任务可以是:

  • 自然语言 prompt
  • 技能调用
  • 通过 RPC 调用的 Python 脚本
# 自然语言任务
--task "Summarize today's activities from memory"

# 技能调用
--task "/daily-report"

# Python 脚本
--task "python:scripts/backup.py"

投递到平台

指定投递目标

# 投递到 Telegram
hermes cron add "daily at 9am" --task "report" --deliver telegram

# 投递到 Discord
hermes cron add "weekly friday 5pm" --task "summary" --deliver discord

# 投递到 CLI(写入文件)
hermes cron add "hourly" --task "check" --deliver file:reports/

投递配置

cron:
  delivery:
    telegram:
      enabled: true
      chat_id: "-100123456789"
    discord:
      enabled: true
      channel_id: "123456789"

投递流程

sequenceDiagram
    participant S as 调度器
    participant H as Hermes
    participant O as 输出处理
    participant P as 平台

    S->>H: 触发任务
    H->>O: 生成输出
    O->>P: 格式化为平台消息
    P->>P: 发送

    Note over P: Telegram / Discord / Slack / CLI

    style P fill:#fff3e0

任务类型

日报

hermes cron add "daily at 9am" --task "Generate daily progress report:
- List completed tasks from memory
- Summarize key accomplishments
- Identify blockers
- Suggest priorities for today" --deliver telegram

夜间备份

hermes cron add "daily at 2am" --task "python:scripts/backup_db.py" --deliver file:backups/

每周审计

hermes cron add "weekly monday 10am" --task "Audit last week:
- Review all task completions
- Identify patterns and inefficiencies
- Generate improvement recommendations" --deliver slack

每小时健康检查

hermes cron add "hourly" --task "Check:
- API connectivity
- Memory usage
- Model availability
Report any issues" --deliver telegram --alert-only

自定义任务示例

任务类型 Prompt 调度
代码审查提醒 Review pending PRs from GitHub 每天 10 点
会议准备 Summarize documents for today's meetings 每天 8:30
支出记录 Log expenses from last 24 hours 每天 18 点
学习提醒 Teach me one concept from my notes 每周日

计划管理

查看全部计划

hermes cron list

输出示例:

ID      Schedule           Task                 Delivery     Next Run
───────────────────────────────────────────────────────────────────────
1       daily 9am          daily report         telegram     2026-04-14 09:00
2       weekly fri 5pm     weekly summary       discord      2026-04-19 17:00
3       hourly             health check         telegram     2026-04-13 17:00

暂停一个计划

hermes cron pause 1

恢复一个计划

hermes cron resume 1

删除一个计划

hermes cron remove 1

编辑一个计划

hermes cron edit 1 --schedule "daily at 10am" --deliver slack

监控

查看执行历史

hermes cron history

输出示例:

Task ID   Executed          Status    Output Size
─────────────────────────────────────────────────────────
1         2026-04-13 09:00  Success   1.2KB
1         2026-04-12 09:00  Success   1.1KB
1         2026-04-11 09:00  Failed    API error
3         2026-04-13 17:00  Success   0.5KB

查看日志

hermes cron logs 1 --last 10

失败告警

cron:
  alerts:
    on_failure: true
    on_timeout: true
    delivery: telegram

高级特性

条件执行

hermes cron add "daily 9am" --task "report" --condition "memory.has_activities"

仅当 Hermes 记忆中存在活动记录时才执行。

失败重试

cron:
  retry:
    max_attempts: 3
    interval: 5m

时区支持

hermes cron add "daily 9am" --task "report" --timezone "America/New_York"

常见问题排查

计划没有执行

原因:守护进程未运行。

解决方案

hermes cron daemon start
hermes cron status

输出没有投递成功

原因:平台未正确配置。

解决方案

hermes gateway status
hermes config set cron.delivery.telegram.enabled true

触发时间不对

原因:时区配置不一致。

解决方案

hermes config set cron.timezone "UTC"
hermes cron edit 1 --timezone "America/New_York"

总结

cron 调度提供了无人值守自动化能力

  1. 自然语言定义 — 更容易创建计划
  2. 平台投递 — 结果可发送到 Telegram、Discord 等平台
  3. 灵活任务类型 — Prompt、技能或脚本都可以
  4. 监控能力 — 历史、日志、告警
  5. 管理能力 — 暂停、恢复、编辑、删除

关键要点

  • ✅ 自然语言让调度更容易上手
  • ✅ 结果可以投递到指定平台
  • ✅ 日报、备份、审计是常见模式
  • ✅ 可通过 hermes cron history 监控执行情况

系列导航: