crontab 常用命令:
查看定时任务 crontab -l
编辑定时任务 crontab -e
crontab 定时设置:
[root@xxx ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
[例]
每天早上6点执行
00 06 * * * echo "Good morning.Today is `date "+%A"`." >> /tmp/reminder.txt
每两个小时执行
0 */2 * * * echo "Have a break now." >> /tmp/reminder.txt
晚上11点到早上7点之间每两个小时和早上8点执行
0 23-7/2,8 * * * /bin/test.sh
每个月的8号和每周的周一至周三的早上10点
0 10 8 * 1-3 command line
每月1日和每周的周一早上7点
0 7 1 1-12 1 echo "Make planning." >> /tmp/reminder.txt
每小时的01分钟执行/etc/cron.hourly内的脚本
01 * * * * root run-parts /etc/cron.hourly
每天4:00执行/etc/cron.daily内的脚本
0 4 * * * root run-parts /etc/cron.daily
每星期日20:00执行/etc/cron.weekly内的脚本(注:周日可以写0或7)
0 20 * * 0 root run-parts /etc/cron.weekly
每月21号22:23分执行/etc/cron.monthly内的脚本
23 22 21 * * root run-parts /etc/cron.monthly
注: run-parts命令位于/usr/bin/run-parts,内容是遍历目标文件夹的shell脚本,执行第一层目录下的有可执行权限的文件。
相关资料:
Crontab详细用法-定时任务详解 https://www.cnblogs.com/xiao-lei/p/11084578.html
Linux下的crontab定时执行任务命令详解 https://www.cnblogs.com/yanzi-meng/p/9156812.html