⏱️
← 返回教學列表

30 個常用 Cron 排程範例:從每 5 分鐘到每月

· 標籤: cron-examples, cron-schedule, crontab-examples, automation-schedule, linux-cron

30 個常用 Cron 排程範例

Cron 表達式是 Linux 自動化的基礎。本指南提供 30 個即用型 cron 範例,依頻率分類,涵蓋開發人員和系統管理員最常見的自動化場景。

每分鐘到每小時

1-2. 每分鐘 / 每 5 分鐘

* * * * * /path/to/script.sh     # 每天執行 1,440 次
*/5 * * * * /path/to/script.sh   # 健康檢查、佇列處理

3-5. 每 10 / 15 / 30 分鐘

*/10 * * * * /path/to/script.sh  # 監控儀表板
*/15 * * * * /path/to/script.sh  # 資料同步
*/30 * * * * /path/to/script.sh  # 工作階段清理

6-8. 每 1 / 2 / 6 小時

0 * * * * /path/to/script.sh     # 每小時整點,日誌輪替
0 */2 * * * /path/to/script.sh   # 午夜、凌晨 2 點、凌晨 4 點批次作業
0 */6 * * * /path/to/script.sh   # 憑證續約檢查

每日排程

9-13. 每日特定時間

0 0 * * * /path/to/script.sh     # 午夜 — 備份、資料庫傾印
0 3 * * * /path/to/script.sh     # 凌晨 3 點 — 完整系統備份
0 6 * * * /path/to/script.sh     # 早上 6 點 — 晨間報表
0 12 * * * /path/to/script.sh    # 中午 — 中午 API 同步
30 23 * * * /path/to/script.sh   # 晚上 11:30 — 日終帳務處理

14-15. 每日多次

0 6,18 * * * /path/to/script.sh  # 每日兩次,早上 6 點和下午 6 點
0 8,12,16,20 * * * /script.sh   # 營業時間每 4 小時

每週排程

16-18. 每週特定日期

0 8 * * 1 /path/to/script.sh     # 週一早上 8 點 — 週報
0 17 * * 5 /path/to/script.sh    # 週五下午 5 點 — 週末備份
0 0 * * 0 /path/to/script.sh     # 週日午夜 — 系統更新

19-21. 工作日排程

0 9 * * 1-5 /path/to/script.sh   # 工作日早上 9 點 — 啟動監控
30 17 * * 1-5 /path/to/script.sh # 工作日下午 5:30 — 結算報表
0 9-17 * * 1-5 /path/to/script.sh# 工作日 9 點到 5 點每小時

每月和定期排程

22-24. 每月排程

0 0 1 * * /path/to/script.sh          # 每月 1 日 — 發票
0 8 15 * * /path/to/script.sh         # 15 日 — 月中報表
0 0 28,29,30,31 * * /path/to/script.sh# 最後一天(較短月份會跳過)

25-27. 每季和每年

0 0 1 1,4,7,10 * /path/to/script.sh  # 季度審查
0 0 15 */3 * /path/to/script.sh      # 每 3 個月
0 0 1 1 * /path/to/script.sh         # 每年 — SSL 續約

28-29. 每週兩次和工作日間隔

0 8 * * 1,4 /path/to/script.sh       # 週一和週四
*/15 9-17 * * 1-5 /path/to/script.sh # 工作日每 15 分鐘

30. 串聯指令

0 6 * * * /path/to/backup.sh && /path/to/cleanup.sh

第二個指令僅在第一個成功時執行。

建立你的 Crontab

# 分鐘  小時  日  月  星期  指令
  */5     *     *    *      *        /opt/scripts/health-check.sh
  0       6     *    *      *        /opt/scripts/daily-backup.sh
  0       18    *    *      1-5      /opt/scripts/eod-report.sh
  0       0     1    *      *        /opt/scripts/monthly-cleanup.sh

執行 crontab -e 來新增條目。cron 守護程序會自動載入新的排程。

部署前先測試

務必使用 cron 表達式驗證器檢查新的 cron 表達式。預覽接下來的 20 次執行時間,確認你的排程行為符合預期。

30 個常用 Cron 排程範例:從每 5 分鐘到每月 - CoolTool