格式

* * * * * root command

第一列的 “*” 为分钟 从 1~59
第二列的 “*” 为小时 从 0~23,0 代表午夜 12 点
第三列的 “*” 为日 从 1~31
第四列的 “*” 为月 从 1~12
第五列的 “*” 为星期 从 0~6,0 代表星期天
第六列的 “root” 为执行的用户
第七列的 “command” 为要运行的命令

分 时 日 月 星期 用户名 命令

范例

[root@localhost ~]# crontab -l
25 5 25 * * root /pki/certbot/certbot-auto renew >> /var/log/le-renew.log
分(0-59)时(0-23)天(1-31)月(1-12)周(0-7;0,7 可使用 sunday)命令(或脚本执行)

以上定时任务表示为:
每个(月)25 日(经过的时候),5 点 25 时使用 root 用户执行 /pki/certbot/certbot-auto renew 命令,并将日志输出到 /var/log/le-renew.log 文件。

Crontab 使用实例

30 21 * * * root reboot
每晚的 21:30 重启服务器。
45 4 1,10,22 * * root reboot
每月 1、10、22 号的 4:45 重启服务器
10 1 * * 6,0 root reboot
每周六、周日的 1:10 重启服务器。
0,30 18-23 * * * root reboot
每天 18:00 至 23:00 之间每隔 30 分钟重启服务器。
0 23-7/1 * * * root reboot
晚上 11 点到早上 7 点之间,每隔一小时重启服务器。
* * * * * root /scripts/script.sh
每分钟执行一次任务
通常情况下,我们并没有每分钟都需要执行的脚本 (默默的想到了 12306--)
0 17 * * sun root /scripts/script.sh
每周日 17:00 执行任务
*/10 * * * * root /scripts/monitor.sh
每 10min 执行一次任务
* * * jan,may,aug * root /script/script.sh
在特定的某几个月执行任务
0 17 * * sun,fri root /script/scripy.sh
在特定的某几天执行任务,在每周五、周日的 17 点执行任务
0 2 * * sun [ $(date +%d) -le 07 ] && root /script/script.sh
在某个月的第一个周日执行任务
0 */4 * * * root /scripts/script.sh
每四个小时执行一个任务
0 4,17 * * sun,mon root /scripts/script.sh
每周一、周日执行任务
* * * * * root /scripts/script.sh
* * * * * sleep 30; root /scripts/script.sh
每个 30 秒执行一次任务,这里没有办法直接通过上诉类似的例子去执行,因为最小的是 1min。但是可以通过如上的方法。
* * * * * root /scripts/script.sh; /scripts/scrit2.sh
多个任务在一条命令中配置
@yearly root /scripts/script.sh
每年执行一次任务,@yearly 类似于 “0 0 1 1 *”。它会在每年的第一分钟内执行。
0 0 1 * * root /scripts/script.sh
每月执行一次任务
0 0 * * 0 root /scripts/script.sh
每周执行一次任务
0 0 * * * root /scripts/script.sh
每天执行一次任务
0 * * * * root /scripts/script.sh
每分钟执行一次任务
@reboot root /scripts/script.sh
系统重启时执行
# crontab -l
MAIL=bob
0 2 * * * root /script/backup.sh
将 Cron 结果重定向的特定的账户
# crontab -l
MAIL=rahul
0 2 * * * root /script/backup.sh
将所有的 cron 命令备份到文本文件当中
# crontab -l > cron-backup.txt
# cat cron-backup.txt
MAIL=rahul
0 2 * * * root /script/backup.sh
备份 cron 到文件中
# crontab -r
# crontab -l
no crontab for root
移除当前的 cron
# crontab cron-backup.txt
# crontab -l
MAIL=rahul
0 2 * * * root /script/backup.sh
从 text file 中恢复

友情提示:本站所有文章,如无特殊说明或标注,均为何星星原创发布。与此同时,趋于近年来本站的文章内容频繁被他站盗用与机器采集,现已全局禁用网站文字内容操作,了解详情或转载文章请 点此 继续!
分类: 系统运维

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注