centos服务器上的Cron作业问题



在我们的生产服务器上有许多通过cron作业执行的进程。决定运行进程的时间是每50分钟之后。

Crontab文件条目-:

*/50 * * * * /usr/bin/php  /webuser/production/class/abc.php
*/50 * * * * /usr/bin/php  /webuser/production/class/abc_1.php
*/50 * * * * /usr/bin/php  /webuser/production/class/abc_2.php

但当我们检查cron日志时,出现了如下所示的错误:

Oct 22 12:56:01 prodserver CROND[40603]: (root) CMD (/usr/bin/php  /webuser/production/class/abc.php )
Oct 22 12:56:01 prodserver CROND[40604]: (CRON) EXEC FAILED (/usr/sbin/sendmail): No such file or directory
Oct 22 12:56:14 prodserver CROND[40602]: (root) MAIL (mailed 1911 bytes of output but got status 0x0001#012)

当我们在命令行/usr/bin/php/webuser/production/class/abc.php上单独运行相同的进程时,它会成功运行。

我们在互联网上浏览,发现一位用户在stackoverflow上回答说,只需让倾向于由cron作业执行的进程将其输出写入一些随机文件,所以我们做了如下所示:

/usr/bin/php  /webuser/production/class/abc.php >> /somerandomfolder/log_cron_check

通过这样做,进程可以通过cron作业在没有错误的情况下执行。尽管通过放置>gt;这只是执行的时间是随机的——这不是crontab中提到的。Cron被提到在50分钟后运行,但我们的数据库和Cron日志显示它在10分钟后意外运行。

queue_start_time
2022-10-22 12:20:01
2022-10-22 12:30:01

请说明我们哪里做错了。

由于没有安装sendmail,crontab日志中出现错误。即使您在PHP代码中不使用sendmail,这又有什么关系呢?crontab有一个实现,可以在完成作业时发送警报通知。来自crontab手册页

如果从crontab条目执行的命令没有重定向标准输出和标准错误,则任何生成的输出或错误都应通过实现定义的方法邮寄给用户。

如前所述,您需要重定向stdout和stderr,您只尝试重定向stdout。像这样的东西应该能解决问题。

*/50 * * * * /usr/bin/php /webuser/production/class/abc.php &>/tmp/log

最新更新