AI任务调度服务

一个为AI编程助手设计的后台任务调度系统,支持定时执行shell命令、AI提示和通知提醒,适用于自动化测试、定期监控等场景。

  • by frr149
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 frr149/claude-cron

使用说明

克劳德·克伦

AI编码助手的任务被推迟。MCP服务器+守护进程,允许任何AI编码助手为未来安排任务。

这与 /loop? 已添加克劳德代码2.1.71 /loop 用于会话中的重复任务。它非常适合短暂的监控,但当你关闭终端时就会死亡。claude cron作为操作系统守护进程运行——您的任务在重启后仍能存活,并在您睡眠时执行。 阅读完整的比较.

快速入门

bun install
bun test          # 38 tests
bun run typecheck # tsc --noEmit

Claude代码配置

添加 ~/.claude/settings.json:

{
  "mcpServers": {
    "claude-cron": {
      "command": "bun",
      "args": ["run", "/Users/fernando/code/claude-cron/src/index.ts"],
      "env": {
        "CLAUDE_CRON_DB": "/Users/fernando/.claude/claude-cron.sqlite",
        "GMAIL_ADDRESS": "frr@keepcoding.io",
        "GMAIL_APP_PASSWORD": "op://FRR DEV/...",
        "NTFY_TOPIC": "claude-cron-frr-xxx",
        "NTFY_URL": "https://ntfy.sh"
      }
    }
  }
}

建筑

AI Assistant (Claude Code / Codex / Copilot)
    │ stdio (MCP protocol)
    ▼
MCP Server (src/index.ts)          ← Task CRUD
    │ SQLite (WAL mode)
    ▼
Runner / Daemon (src/runner.ts)    ← Loop: executes due tasks
    ├─ reminder  → notification + email + push
    ├─ shell     → Bun.spawn(command)
    ├─ claude    → claude -p --output-format text "prompt"
    ├─ codex     → codex --quiet "prompt"
    └─ copilot   → gh copilot suggest "prompt"

MCP工具

工具 说明
schedule_task 为未来安排任务
list_tasks 列出任务(按状态、即将进行、标签筛选)
cancel_task 按ID取消任务
run_now 标记任务以立即执行
install_daemon 将运行者注册为操作系统服务

调度任务

{
  name: "Nightly tests",
  type: "shell",              // reminder | shell | claude | codex | copilot
  when: "daily at 3:00",      // see "When expressions" below
  payload: {
    command: "cd ~/code/bfclaude && make test",  // shell
    // message: "...",         // reminder
    // prompt: "...",          // claude/codex/copilot
    // model: "haiku",
    // workingDir: "/path",
    // maxBudgetUsd: 0.10,
  },
  notify: ["notification", "email", "push"],
  notifyTo: "frr@keepcoding.io",
  tags: ["ci"],
}

when 表达方式

类型 示例
自然语言 "tomorrow 9:00", "in 30 minutes", "next friday at 3pm"
Cron(5个字段) "0 9 * * 1", "*/5 * * * *"
ISO 8601 "2026-03-01T09:00:00Z", "2026-03-01"
快捷方式 "every monday at 8", "daily at 9:00", "every 5 minutes"

解析:时间节点(自然)+croner(cron)+正则表达式(快捷方式)。快捷方式在chrono节点之前进行评估,因此“每周一8点”不会被解释为单个日期。

结构

src/
├── index.ts              # MCP server entry point (5 tools)
├── runner.ts             # Daemon: oneshot (launchd) or --loop (dev)
├── queue.ts              # SQLite: schema, CRUD, getDueTasks
├── scheduler.ts          # parseWhen(): chrono-node + croner + shortcuts
├── adapters/
│   ├── types.ts          # AIAdapter interface
│   ├── claude.ts         # claude -p --output-format text
│   ├── codex.ts          # codex --quiet (stub)
│   └── copilot.ts        # gh copilot suggest (stub)
├── notify/
│   ├── types.ts          # NotificationAdapter interface
│   ├── desktop.ts        # node-notifier (cross-platform)
│   ├── email.ts          # Direct SMTP to Gmail (TLS 465)
│   └── push.ts           # ntfy.sh (mobile + Apple Watch)
└── daemon/
    ├── types.ts          # DaemonInstaller interface
    ├── detect.ts         # process.platform → installer
    ├── launchd.ts        # macOS: ~/Library/LaunchAgents/
    ├── systemd.ts        # Linux: ~/.config/systemd/user/
    └── schtasks.ts       # Windows: Task Scheduler

跑者

两种模式:

# Oneshot (for launchd StartInterval / systemd timer)
bun run src/runner.ts

# Continuous loop (for dev or KeepAlive)
bun run src/runner.ts --loop

守护进程每60秒读取一次SQLite(可通过以下方式配置 CLAUDE_CRON_INTERVAL),执行到期任务并发送通知。

Cron任务在完成后会自动重新安排时间(next_run重新计算)。一次性任务过渡到 completed 或 failed.

守护进程

# Install (from Claude Code via MCP tool install_daemon, or manually):
# macOS:
bun run src/daemon/launchd.ts  # generates plist + launchctl load

# Verify
launchctl list | grep claude-cron
tail -f ~/Library/Logs/claude-cron.log

# Uninstall
launchctl unload ~/Library/LaunchAgents/com.claude-cron.runner.plist

通知

渠道 要求 环境变量
桌面 节点通知器(包括在内) --
电子邮件 Gmail应用程序密码 GMAIL_ADDRESS, GMAIL_APP_PASSWORD
手机/手表上的推送(ntfy) ntfy应用程序 NTFY_TOPIC, NTFY_URL (可选), NTFY_TOKEN (可选)

SQLite架构

tasks (
  id TEXT PRIMARY KEY,          -- nanoid 12 chars
  name, type, status,           -- type: reminder|shell|claude|codex|copilot
  run_at INTEGER,               -- Unix ts (one-off)
  cron TEXT,                    -- Cron expression (recurring)
  payload TEXT,                 -- JSON per type
  notify_via TEXT,              -- JSON array: ["notification","email","push"]
  notify_to TEXT,               -- Destination email
  next_run, last_run,           -- Timestamps
  run_count, last_result,       -- Executions + JSON result
  created_at, created_by, tags  -- Metadata
)

依赖项

  • @modelcontextprotocol/sdk --MCP协议
  • chrono-node --自然语言→ Date
  • croner --Cron解析+下一次运行
  • nanoid --短ID
  • node-notifier --跨平台桌面通知
  • zod --MCP输入验证
  • bun:sqlite --内置,零存款

待办事项

  • []在real settings.json中配置MCP并进行端到端测试
  • []使用安装守护进程 install_daemon 并验证整个周期
  • []设置ntfy以推送到手机/手表
  • []npm发布(名称待定)
  • []在macOS+Linux上进行CI测试
  • []带实际使用GIF的README