crontab执行时间与系统时间不一致问题分析

7 篇文章 0 订阅
4 篇文章 0 订阅

导致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 在线数: 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值