crontab命令使用
参数解析:
-e:编辑定时任务
-l:查看定时任务
-r:删除定时任务
-u:指定其他用户
crontab的配置文件
vim /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
0 0 * * * sh /usr/local/redis/copy/redis_rdb_copy_daily.sh &> /dev/null
参数解析:
*:表示任意的(分、时、日、月、周)时间都执行
-:表示一个时间范围段,如5-7点
,:表示分隔时段,如6,0,4表示周六、日、四
/1:表示每隔n单位时间,如* /10 每10分钟
具体案例:
00 02 * * * #每天的凌晨2点整执行
00 02 1-8 * * ls #每月1号到8号凌晨2点执行
45 4 1,10,22 * * ls #每月的1,10,22号的4:45执行
0 23-7/1 * * * ls #晚上11点到早上7点之间,每隔一个小时执行
实践:同步服务器时间
#保证服务器环境有ntpdate命令
yum install -y ntpdate
#更改系统时间
[root@service02 bin]# ntpdate ntp.aliyun.com
21 Jun 17:07:15 ntpdate[8377]: step time server 203.107.6.88 offset 2.440287 sec
[root@service02 bin]# crontab -e
*/2 * * * * /usr/sbin/ntpdate ntp.aliyun.com &> /dev/null