导致crontab无法安装系统时间执行的原因注意是时区问题,但是时区改动一定要重启crond,否则不会生效!
注意事项:
1、系统时区不一致导致
2、时区改动后没有重启crond
如下为一次线上事故的发现与分析及解决过程:
#查看定时任务并未按照预期时间执行
[root@itso123 logs]# tail -15 abc.log
2018-08-20 06:54:01 在线数:
2018-08-20 06:56:01 在线数:
2018-08-20 06:58:02 在线数:
2018-08-20 07:09:01 在线数:
2018-08-20 07:19:01 在线数:
2018-08-20 07:29:01 在线数:
2018-08-20 07:39:01 在线数:
2018-08-20 07:49:01 在线数:
2018-08-20 07:59:01 在线数:
2018-08-20 08:59:01 在线数:
2018-08-20 09:59:01 在线数:
2018-08-20 10:59:01 在线数:
2018-08-20 11:59:02 在线数:
2018-08-20 12:59:01 在线数:
2018-08-20 13:59:01 在线数:
#当前系统时间与时区
[root@itso123 logs]# date
2018年 08月 20日 星期一 14:01:26 CST
#crontab配置来看,当前时间应该是每2分钟执行一次,可是日志显示却是1小时执行
8-22点:每2分钟执行
7-8和23点:每10分钟执行一次
0-6点:每1 小时执行一次
[root@itso123 logs]# 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
#system log monitor
*/2 8-22 * * * root /usr/bin/python /home/abc.py >>/data/logs/abc.log
9,19,29,39,49,59 7-8,23 * * * root /usr/bin/python /home/abc.py >>/data/logs/abc.log
59 0-6 * * * root /usr/bin/python /home/abc.py >>/data/logs/abc.log
#时区配置一致(难得是没有重启crond服务导致?)
[root@itso123 logs]# more /etc/localtime
TZif2
CST-8
[root@itso123 logs]# cat /usr/share/zoneinfo/Asia/Shanghai
TZif2�\���'p�����Z��6ip ~h�!Iap"^J�#)Cp$Gg%_�&'I&�A�(+(�#�~�p�CDTCSTTZif2
����~6C)�����\�������'p�������������Z��6ip ~h�!Iap"^J�#)Cp$Gg%_�&'I&�A�(+(�#�q�~�pLMTCDTCST
CST-8
#时区配置晚于crond启动时间(果然是配置改动后未重启!)
[root@itso123 logs]# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 8月 2 19:51 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
[root@itso123 logs]# ps -ef|grep cron
root 5004 1 0 7月26 ? 00:00:08 /usr/sbin/crond -n
root 21253 8440 0 14:07 pts/1 00:00:00 grep --color=auto cron
#重启crond服务
[root@itso123 logs]# service crond restart
Redirecting to /bin/systemctl restart crond.service
[root@itso123 logs]# ps -ef|grep cron
root 21524 1 6 14:08 ? 00:00:00 /usr/sbin/crond -n
root 21544 8440 0 14:08 pts/1 00:00:00 grep --color=auto cron
#crontab已经生效为每2分钟执行
[root@itso123 logs]# tail -f monitor.log
2018-08-20 14:10:02 在线数:
2018-08-20 14:12:01 在线数:
2018-08-20 14:14:01 在线数: