
高效运维 !!! 20个定时命令必备
2025年07月17日 21:02
以下文章来源于开源运维 ,作者开源运维
分享优质GitHub开源项目的安装教程。 在现代系统运维中,任务调度是必不可少的一部分。无论是定时备份、日志清理,还是自动化脚本运行,Linux 的 crontab 都是你绕不开的工具。本文通过20个实用的 crontab 例子,全面解析其使用技巧,帮助你高效完成系统运维工作。 Crontab 是 Linux 下的任务定时调度工具,用于指定周期性执行的任务。在学习这些例子之前,需要注意以下基础知识: 编辑 crontab 任务: 查看所有任务列表: 以下是20个实际场景的 Crontab 用法,涵盖了日常运维需要的各种任务调度。 日志路径: 调试命令: Crontab 是 Linux 系统运维的得力助手,掌握这些常见配置可以让你的任务调度更加高效和自动化。在实际工作中,不断根据需求改进和定制化这些配置,才能充分发挥其潜能。 开源运维 .
什么是Crontab?
分 时 日 月 周 命令
*
:任意值,
:分隔多个时间点-
:指定范围/
:指定步长crontab -e
crontab -l
20个Crontab实例
0 1 * * * /path/to/script.sh
0 10,22 * * * /path/to/script.sh
0 1 * * 0 /path/to/backup.sh
* * * * * /path/to/script.sh
*/5 * * * * /path/to/script.sh
0 * * * * /path/to/script.sh
0 */2 * * * /path/to/script.sh
0 0 * * * /path/to/script.sh
0 0 */2 * * /path/to/script.sh
0 13 * * 2,4 /path/to/script.sh
@weekly /path/to/script.sh
0 11 15 * * /path/to/script.sh
@monthly /path/to/script.sh
0 0 * 1,4,6 * /path/to/script.sh
@reboot /path/to/script.sh
MAILTO="user@example.com"
1 1 * * * /path/to/script.shSHELL=/bin/bash
1 1 * * * /path/to/script.shPATH=/usr/local/bin:/usr/bin:/bin
1 1 * * * /path/to/script.shHOME=/
1 1 * * * /path/to/script.sh
虽然 crontab 本身不支持秒级调度,可以结合 while
循环实现:* * * * * while true; do /path/to/script.sh; sleep 1; done
关键日志与错误排查
/var/log/cron
是默认的任务日志文件,可在这里查看任务是否正常执行。
如果任务未执行,尝试手动执行测试是否出错:sh /path/to/script.sh