⏱️
← 返回教程列表

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 # 工作日 17:30 —— 收盘报告
0 9-17 * * 1-5 /path/to/script.sh# 每小时,工作日 9 点到 17 点

每月与周期性调度

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