阿里云 Ubuntu 16.04.7 LTS 镜像 cron.daily 不执行 BUG 排除过程
/etc/cron.daily 下面任务不执行
事情是这样的,我有一个数据库备份脚本,丢进 /etc/cron.daily 过了几天回来一看,妈呀!!!一个备份都没有产生。这到底是什么原因?
建议你收藏,转发,如果你认为这次故障处理对你有帮助,请点点小红心。
检查日志并无异常
root@production:~# cat /var/log/syslog.1 | grep dailyAug 24 11:59:01 production CRON[31423]: (root) CMD ( test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond)Aug 25 06:25:01 production CRON[23913]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ))
/etc/crontab 配置无误
root@production:~# cat /etc/crontab# /etc/crontab: system-wide crontab# Unlike any other crontab you don't have to run the `crontab'# command to install the new version when you edit this file# and files in /etc/cron.d. These files also have username fields,# that none of the other crontabs do.SHELL=/bin/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m h dom mon dow user command17 * * * * root cd / && run-parts --report /etc/cron.hourly25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )#
检查 /usr/sbin/anacron
root@production:~# /usr/sbin/anacron-bash: /usr/sbin/anacron: No such file or directory
终于定位到问题了,很多云主机的镜像是经过裁剪的,这样是为了降低镜像的尺寸,但是裁剪过程中难免考虑不周。
此故障出现在阿里云 Ubuntu 16.04.7 LTS 镜像中。
root@production:~# cat /etc/issueUbuntu 16.04.7 LTS \n \l
anacron 是用于协调 cron 的,cron 的设计是7*24小时工作的,对于很多非服务器晚上会关机,那么有些 cron 就会错过执行时间,anacron 就可以协调 cron 当开机后 anacron 会判断哪些计划任务被错过,并补救执行。显然对于7*24小时运行的服务器来说 anacron 并没有卵用,所以阿里云把它卸载掉,但并未考虑到会影响 cron 执行。
解决方案,有两种:一、创建一个空文件,骗过 test -x /usr/sbin/anacron 命令。二、修改 /etc/crontab 配置文件,去掉 test -x /usr/sbin/anacron ||。
root@production:~# vim /etc/crontabroot@production:~# cat /etc/crontab# /etc/crontab: system-wide crontab# Unlike any other crontab you don't have to run the `crontab'# command to install the new version when you edit this file# and files in /etc/cron.d. These files also have username fields,# that none of the other crontabs do.SHELL=/bin/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m h dom mon dow user command17 * * * * root cd / && run-parts --report /etc/cron.hourly25 6 * * * root cd / && run-parts --report /etc/cron.daily47 6 * * 7 root cd / && run-parts --report /etc/cron.weekly52 6 1 * * root cd / && run-parts --report /etc/cron.monthly#
这是我最终修改后的版本,记得重启 cron 服务
root@production:~# systemctl restart cronroot@production:~# systemctl status cron● cron.service - Regular background program processing daemonLoaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)Active: active (running) since Wed 2021-08-25 10:20:27 CST; 4s agoDocs: man:cron(8)Main PID: 29414 (cron)Tasks: 1Memory: 384.0KCPU: 1msCGroup: /system.slice/cron.service└─29414 /usr/sbin/cron -fAug 25 10:20:27 production systemd[1]: Started Regular background program processing daemon.Aug 25 10:20:27 production cron[29414]: (CRON) INFO (pidfile fd = 3)Aug 25 10:20:27 production cron[29414]: (CRON) INFO (Skipping @reboot jobs -- not system startup)
文章转载自netkiller,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




