crontab -r误删定时任务

错误现象

误执行**导致定时任务被删除

[root@ennan ~]# crontab -r
no crontab for root
[root@ennan ~]# crontab -l
no crontab for root

解决思路

查看crontab的日志,根据日志的规律推算出原有的定时任务

[root@ennan ~]# cat /var/log/cron
Aug 26 20:20:01 ennan CROND[8686]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null)
Aug 26 20:21:01 ennan CROND[8699]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:22:01 ennan CROND[8707]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:23:01 ennan CROND[8715]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:24:01 ennan CROND[8723]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:25:01 ennan CROND[8733]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:25:01 ennan CROND[8734]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null)
Aug 26 20:26:01 ennan CROND[8746]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:27:01 ennan CROND[8754]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:28:01 ennan CROND[8762]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:29:01 ennan CROND[8770]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:30:01 ennan CROND[8783]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null)
Aug 26 20:30:01 ennan CROND[8782]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 26 20:30:01 ennan CROND[8784]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:31:01 ennan CROND[8798]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:32:01 ennan CROND[8806]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:33:01 ennan CROND[8814]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:34:01 ennan CROND[8822]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:35:01 ennan CROND[8833]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null)
Aug 26 20:35:01 ennan CROND[8832]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:36:01 ennan CROND[8845]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:37:01 ennan CROND[8853]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:38:01 ennan CROND[8861]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:39:01 ennan CROND[8869]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:40:01 ennan CROND[8881]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 26 20:40:01 ennan CROND[8884]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null)
Aug 26 20:40:01 ennan CROND[8886]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:41:01 ennan CROND[8897]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:42:02 ennan CROND[8906]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:43:01 ennan CROND[8914]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:44:01 ennan CROND[8922]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:45:01 ennan CROND[8932]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null)
Aug 26 20:45:01 ennan CROND[8933]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:45:57 ennan crontab[8942]: (root) BEGIN EDIT (root)
Aug 26 20:46:01 ennan CROND[8946]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:47:01 ennan CROND[8954]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)
Aug 26 20:48:01 ennan CROND[8964]: (root) CMD (/bin/bash /root/ip_data.sh &> /dev/null)

由日志分析可得出:
每分钟执行一次/bin/bash /root/ip_data.sh &> /dev/null
每5分钟执行一次(/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null

真实的生成环境中,需要仔细的查看日志,且要跨较长的时间周期,尽最大程度的恢复。

故障恢复

根据推测结果,恢复定时任务

[root@ennan ~]# crontab -l
*/1 * * * * /bin/bash /root/ip_data.sh &> /dev/null
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null

其实定时任务存储于var/spool/cron/{usernmae}中,为了防止此类错误出现,可提前将这个文件备份

[root@ennan ~]# cat /var/spool/cron/root
*/1 * * * * /bin/bash /root/ip_data.sh &> /dev/null
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null

你可能感兴趣的:(crontab -r误删定时任务)