centos7上crontab不执行
前言
今天发现在centos7上面 crontab 任务不执行,直接手动执行就可以运行成功
查看crontab日志
命令:
shell
tail -f /var/log/cron
tail -f /var/log/cron
输出:
shell
Aug 28 19:18:01 NWqrPX209319 CROND[41152]: (root) MAIL (mailed 79 bytes of output but got status 0x004b#012)
Aug 28 19:20:01 NWqrPX209319 CROND[41164]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 28 19:30:01 NWqrPX209319 CROND[41178]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 28 19:31:35 NWqrPX209319 crontab[41451]: (root) LIST (root)
Aug 28 19:31:35 NWqrPX209319 crontab[41451]: PAM pam_end: NULL pam handle passed
Aug 28 19:31:40 NWqrPX209319 crontab[41452]: (root) BEGIN EDIT (root)
Aug 28 19:31:51 NWqrPX209319 crontab[41452]: (root) REPLACE (root)
Aug 28 19:31:52 NWqrPX209319 crontab[41452]: (root) END EDIT (root)
Aug 28 19:31:52 NWqrPX209319 crontab[41452]: PAM pam_end: NULL pam handle passed
Aug 28 19:32:01 NWqrPX209319 crond[16613]: (root) RELOAD (/var/spool/cron/root)
Aug 28 19:36:01 NWqrPX209319 CROND[41460]: (root) CMD (sh /data/www/56bird_cn/crawler/publish_job.sh >> /data/www/56bird_cn/publish.log)
Aug 28 19:36:01 NWqrPX209319 CROND[41459]: (root) MAIL (mailed 79 bytes of output but got status 0x004b#012)
Aug 28 19:18:01 NWqrPX209319 CROND[41152]: (root) MAIL (mailed 79 bytes of output but got status 0x004b#012)
Aug 28 19:20:01 NWqrPX209319 CROND[41164]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 28 19:30:01 NWqrPX209319 CROND[41178]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 28 19:31:35 NWqrPX209319 crontab[41451]: (root) LIST (root)
Aug 28 19:31:35 NWqrPX209319 crontab[41451]: PAM pam_end: NULL pam handle passed
Aug 28 19:31:40 NWqrPX209319 crontab[41452]: (root) BEGIN EDIT (root)
Aug 28 19:31:51 NWqrPX209319 crontab[41452]: (root) REPLACE (root)
Aug 28 19:31:52 NWqrPX209319 crontab[41452]: (root) END EDIT (root)
Aug 28 19:31:52 NWqrPX209319 crontab[41452]: PAM pam_end: NULL pam handle passed
Aug 28 19:32:01 NWqrPX209319 crond[16613]: (root) RELOAD (/var/spool/cron/root)
Aug 28 19:36:01 NWqrPX209319 CROND[41460]: (root) CMD (sh /data/www/56bird_cn/crawler/publish_job.sh >> /data/www/56bird_cn/publish.log)
Aug 28 19:36:01 NWqrPX209319 CROND[41459]: (root) MAIL (mailed 79 bytes of output but got status 0x004b#012)
我们可以清楚的看到,crontab是有执行的,但是执行失败了。发邮件通知,但是邮件没有配置好,所以邮件发送失败了。我们只要打开邮件的日志,就能知道是什么错误
开启crontab邮件记录
修改配置:
shell
vim /etc/postfix/main.cf
vim /etc/postfix/main.cf
发现配置为:
shell
inet_interfaces = localhost
inet_protocols = all
inet_interfaces = localhost
inet_protocols = all
改为:
shell
inet_interfaces = all
inet_interfaces = all
重启:
- centos7
shell
systemctl restart postfix.service
systemctl restart postfix.service
- centos6
shell
service postfix restart
service postfix restart
等待crontab再次执行任务,然后查看日志:
shell
tail -f /var/spool/mail/root
tail -f /var/spool/mail/root
如果maill 有日志的时候,也会在控制台弹出这样的提示:
log
您在 /var/spool/mail/root 中有邮件
您在 /var/spool/mail/root 中有邮件
根据邮件内容的错误修改,就能正常执行crontab的任务了。