Claude Code Scheduled Triggers:讓 AI 定時自動幫你做事


Claude Code 不只是一個「你問它答」的工具。它可以定時自動執行任務——不需要你在場、不需要你下指令。

這篇整理 Claude Code 目前提供的四種定時觸發機制,從最輕量的 /loop 到最持久的 Cloud Tasks,以及各自適合的使用場景。


四種定時觸發機制

機制生命週期適合場景
/loopSession 內(關掉就沒了)開發中反覆檢查
/schedule持久化(排程存檔)跨 session 的定期任務
Desktop Tasks桌面應用背景執行長期自動化工作流
Cloud Tasks雲端(claude.ai/code)不需要本機開著

/loop:Session 內的輪詢

/loop 是最簡單的定時觸發——在當前 session 中,讓 Claude Code 每隔一段時間自動執行一次任務。

基本用法

/loop every 5 minutes: check if the build passes

Claude Code 會每 5 分鐘跑一次 npm run build(或你專案的建置指令),如果失敗就通知你。

實用範例

監控測試是否通過:

/loop every 10 minutes: run the test suite, if any test fails, 
summarize which tests broke and likely causes

檢查 PR 狀態:

/loop every 15 minutes: check if there are any new comments 
on my open PRs using gh cli

監控日誌:

/loop every 3 minutes: tail the last 50 lines of app.log, 
flag any ERROR or WARN entries I should know about

限制

  • 最多同時 50 個 /loop 任務
  • Session 關閉就停止——不會跨 session 存活
  • 每個 loop 都消耗 context,太多會影響效能
  • 最短間隔建議 1 分鐘以上

管理 Loop

/loop list          # 查看目前所有 loop
/loop stop <id>     # 停止特定 loop
/loop stop all      # 停止所有 loop

/schedule:持久化的排程任務

/schedule 把任務存到檔案系統,即使關掉 Claude Code 也會在下次啟動時繼續執行。

基本用法

/schedule "daily dependency check" every day at 9am: 
check for outdated npm packages, create a summary of 
packages that are more than 2 major versions behind

Cron 語法支援

/schedule "weekly review" cron 0 9 * * 1: 
review all commits from last week, summarize major changes

標準 cron 格式:分 時 日 月 週

管理排程

/schedule list              # 查看所有排程
/schedule delete <name>     # 刪除特定排程
/schedule pause <name>      # 暫停
/schedule resume <name>     # 恢復

排程儲存位置

排程設定存在專案的 .claude/ 目錄下,可以 commit 到版本庫讓團隊共用。


Desktop Tasks:桌面應用的背景任務

如果你使用 Claude Code 的桌面應用(Mac/Windows),可以建立在背景持續運行的 Tasks。

建立方式

在桌面應用中:

  1. 開啟 Command Palette
  2. 選擇「New Task」
  3. 設定觸發條件和執行內容

與 /schedule 的差異

/scheduleDesktop Tasks
執行環境CLI session桌面應用背景
需要終端機
GUI 管理
通知方式終端機輸出系統通知

Desktop Tasks 適合不常開終端機的使用者,或需要系統通知的場景。


Cloud Tasks:不需要本機的雲端排程

Cloud Tasks 在 claude.ai/code 上運行,不需要你的電腦開著。

建立方式

在 claude.ai/code 的介面中設定,或透過 CLI:

claude task create "nightly code review" \
  --schedule "0 2 * * *" \
  --repo owner/repo \
  --prompt "review all PRs merged today, flag potential issues"

限制

  • 最短間隔 1 小時
  • 需要連接 GitHub repo
  • 每次執行有獨立的 context(不會記得上次的結果)
  • 消耗 API credits

適合場景

  • 每日程式碼品質報告
  • 每週相依套件安全檢查
  • 每日 PR review 摘要
  • CI 失敗自動分析

實戰場景

場景一:自動 PR Review

/schedule "pr-review" every day at 9am:
1. List all open PRs using gh pr list
2. For each PR opened in the last 24 hours:
   - Read the diff
   - Check for common issues (SQL injection, missing error handling, 
     hardcoded secrets)
   - Leave a review comment if issues found

這不會取代人工 code review,但可以在人看之前先抓出明顯問題。

場景二:相依套件安全檢查

/schedule "dep-audit" cron 0 10 * * 1:
1. Run npm audit
2. Run npm outdated
3. If any HIGH or CRITICAL vulnerabilities found, 
   create a GitHub issue with details
4. Summarize packages that need attention

每週一早上 10 點自動檢查,有問題就開 issue。

場景三:CI 失敗分析

/loop every 10 minutes: 
check gh run list for failed runs in the last hour.
If found, analyze the failure logs, identify root cause, 
and suggest a fix.

開發中搭配 /loop 使用,CI 失敗就自動分析原因。

場景四:文件同步檢查

/schedule "doc-sync" every day at 6pm:
compare README.md and docs/ with the actual code.
Flag any documented APIs that no longer exist, 
or new exports that aren't documented.

場景五:Memory 維護

/schedule "memory-cleanup" cron 0 9 * * 5:
review all memory files in .claude/memory/.
Remove entries that reference deleted files or 
outdated project information.

每週五自動清理過期的 memory 記錄。


選擇哪種機制

需要定時觸發?
├── 只在開發中用?
│   └── /loop(Session 內、輕量)
├── 需要跨 session 持續?
│   ├── 有開終端機?
│   │   └── /schedule(持久化排程)
│   └── 不想開終端機?
│       └── Desktop Tasks(桌面應用背景)
└── 不需要本機開著?
    └── Cloud Tasks(雲端排程)

組合使用

實務上可以組合使用:

  • 開發時/loop 監控建置和測試
  • 日常/schedule 做每日 PR review
  • 長期:Cloud Tasks 做每週安全檢查

注意事項

成本控制

每次觸發都消耗 API tokens。高頻率的 /loop(例如每分鐘)加上複雜的任務(例如讀完整個 codebase),一天的 token 消耗可能很可觀。

建議

  • /loop 任務盡量簡單(例如只跑測試、只看 log 最後幾行)
  • 複雜分析用 /schedule 設定較長間隔(每天、每週)
  • 定期用 /loop list 檢查是否有忘記關掉的 loop

安全性

定時任務以你的權限執行。如果任務涉及 git push、建立 issue、或留 PR comment,確認你的 Claude Code 權限設定是正確的。

冪等性

好的定時任務應該是冪等的——跑兩次的結果跟跑一次一樣。例如「檢查是否有問題,有就開 issue」應該先檢查 issue 是否已存在,避免重複開。


結語

定時觸發把 Claude Code 從「被動回答問題」變成「主動幫你盯梢」。最有價值的用法不是讓它做你正在做的事,而是做你忘記做、或沒時間做的事——像是每天看一眼有沒有新的安全漏洞、每週整理一次過期的 TODO。

從一個簡單的 /loop 開始試。當你發現它在你沒注意的時候抓到一個問題,你就會明白為什麼這個功能值得用。