从cron运行php不是作为CLI运行 [英] Running php from cron did not run as CLI
问题描述
我注意到这个问题后php脚本从cron启动到超时运行,但它不是一个问题,当它是从命令行手动运行。 (缺省情况下,PHP的max_execution_time为0)
所以我试图运行一个简单的cron:
50 8 * * * php -q /tmp/phpinfo.php> / tmp / phpinfo
脚本只会调用phpinfo()。
令人惊讶的是,它以html格式写出了phpinfo,这表明它不是作为CLI运行的。
从命令行手动运行脚本
code> php -q /tmp/phpinfo.php | less
在文本格式中输出了php信息,输出中的max_execution_time为0。
我知道在某处有一个配置问题,但我只是找不到问题所在。这是发生在生产服务器上,我有一个完全控制。在我的开发机器上从cron运行相同的脚本运行良好。
这里是差异的总结
function | CLI | cron |
php_sapi_name | cli | cgi-fcgi |
php_ini_loaded_file | /usr/local/lib/php.ini | /usr/local/lib/php.ini |
我怀疑你的问题在于缺少环境变量,特别是所有重要的 $ PATH
。当您执行此操作时:
php -q /tmp/phpinfo.php
系统必须通过 php
来计算你的意思。它通过查找当前 $ PATH
环境变量中的目录来执行此操作。
从正常的shell执行,您的环境设置的方式,它找到的PHP版本的PHP,如你所期望的。
但是,当 cron
执行一个命令时,没有交互式shell将设置的所有环境变量。因为在您的系统上可能有其他可执行文件 php
,对于不同的SAPIs,它可能会选择错误的 - 在你的情况下,<$ c $根据您从 php_sapi_name()
要解决这个问题,首先通过输入以下内容在正常shell中找到正确的 php
可执行文件的路径:
其中php
code> / usr / bin / php 。您可以进一步检查这是否实际上是指向不同文件名的符号链接:
ls -l $ (这个php)
(如果输出中有一个箭头, c $ c> / usr / bin / php - > / usr / bin / php5-cli )
并且在你的crontab条目中使用它,所以它看起来像这样:
50 8 * * * / usr / bin / php5-cli -q /tmp/phpinfo.php> / tmp / phpinfo
I noticed this problem after a php script ran from cron started to timeout but it was not an issue when it was ran manually from command line. (PHP has max_execution_time is 0 for CLI by default)
So I tried to run a simple cron such:
50 8 * * * php -q /tmp/phpinfo.php > /tmp/phpinfo
The script would just call phpinfo().
Surprisingly it wrote out phpinfo in html format, which suggested that it was not run as CLI. And max_execution_time was 30 in the output.
Running the script manually from command line such
php -q /tmp/phpinfo.php | less
wrote out the php info in text format and max_execution_time was 0 in the output.
I know there must be a configuration issue somewhere, but I just could not find where the problem is. This is happening on a production server, which I have a complete control of. Running the same script from cron on my development machine worked fine.
Here is the summary of the difference
function | CLI | cron |
php_sapi_name | cli | cgi-fcgi |
php_ini_loaded_file | /usr/local/lib/php.ini | /usr/local/lib/php.ini |
I suspect your problem lies in a missing environment variable, specifically the all-important $PATH
. When you run this:
php -q /tmp/phpinfo.php
the system must work out what program you mean by php
. It does this by looking, in order, through the directories in the current $PATH
environment variable.
Executed from a normal shell, your environment is set up in such a way that it finds the CLI version of PHP, as you expect.
However, when cron
executes a command, it does so without all the environment variables that your interactive shell would set up. Since there will probably be other executables called php
on your system, for different "SAPIs", it may pick the "wrong" one - in your case, the cgi-fcgi
executable, according to the output you report from php_sapi_name()
.
To fix this, first find the path to the correct php
executable in a normal shell by typing this:
which php
This should give you a path like /usr/bin/php
. You can go one further and check if this is actually a "symbolic link" pointing at a different filename:
ls -l $(which php)
(you'll see an arrow in the output if it is, like /usr/bin/php -> /usr/bin/php5-cli
)
Then take this full path to the PHP executable and use that in your crontab entry, so it looks something like this:
50 8 * * * /usr/bin/php5-cli -q /tmp/phpinfo.php > /tmp/phpinfo
这篇关于从cron运行php不是作为CLI运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!