如果由Cron运行,PHP`exec()`不起作用 [英] PHP `exec()` doesn't work if run by Cron
问题描述
我有一个crontab每分钟调用一次的PHP脚本,如下所示:
I have a PHP script that is being called every minute by crontab like this:
* * * * * /usr/bin/php /var/www/html/cronjobs/watchdog/watchdogExec.php
脚本执行许多其他PHP脚本并传递如下变量:
The script executes a number of other PHP scripts and passes a variable like so:
$ccmd = "nohup sudo /usr/bin/php watchdog.php " . $var . " &";
exec($ccmd);
从浏览器调用时, watchdogExec.php 运行良好,并且工作。但是,当我添加cronjob时,脚本无法正常工作,并且正在向邮件发送垃圾邮件,并显示以下错误:
When called from a browser, watchdogExec.php runs fine and does its job. However when I added the cronjob, the script is not working and is spamming mails with the following error:
No entry for terminal type "unknown";
using dumb terminal settings.
此错误出现的次数与 exec()$ c相同$ c>被调用,所以我认为这一定是问题所在。我搜索并了解到cronjob在终端中未运行命令的情况,并尝试在crontask中设置环境,但这无济于事。显然PHP正在打印该错误,但是我在他们的文档中没有发现任何有关此错误的信息。
This error appears the same number of times exec()
is called, so I figured that must be the issue. I searched and read about cronjob not running commands in a terminal, and I tried setting the environment in the crontask, but it did not help. Apparently PHP is printing that error, but I haven't found anything in their documentation about it.
* * * * * /usr/bin/env TERM=xterm /usr/bin/php /var/www/html/cronjobs/watchdog/watchdogExec.php
我正在运行CentOS 7。
I am running CentOS 7.
编辑:
进行了更多搜索并找到了了解有关PHP环境变量的信息。有人通过在他们的PHP脚本中添加以下内容来解决类似的问题。但是,这不能解决我的问题,只能停止垃圾邮件。该脚本仍无法正常工作。
Searched some more and found out about PHP environment variables. Someone fixed a similar problem by adding the following in their PHP script. However this is not fixing my problem, it only stops the mail spam. The script is still not working.
putenv('SHELL=/bin/bash');
putenv('TERM=xterm');
推荐答案
您是否也尝试在exec语句中使用完整路径?还可以尝试添加TERM环境变量:
Have you tried using full paths in your exec statement too? Also try adding the TERM environment variable:
$ccmd = "nohup /usr/bin/sudo /usr/bin/env TERM=xterm /usr/bin/php watchdog.php " . $var . " &";
这篇关于如果由Cron运行,PHP`exec()`不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!