无法获取PHP cron脚本运行 [英] Cannot get PHP cron script to run
问题描述
我有一个PHP脚本,我需要运行每一分钟。我已经确保脚本从命令行工作,我使用绝对路径,以避免任何环境问题:
I have a PHP script that I need to run every minute. I have made sure that the script works from the command line, and I'm using absolute paths to avoid any environment issues:
/usr/bin/php -q /var/www/myapp/services/myservice.php
手动从命令行以root身份运行正常工作,正如我可以从我的脚本写入的日志文件中看到的。
Manually running that as root from the command line works fine, as I can see from the log file that my script writes to. To be sure, the script has execute permissions as well.
但是,当在cron中放置完全相同的命令时:
However, when placing the same exact command in a cron:
* * * * * /usr/bin/php -q /var/www/myapp/services/myservice.php
它不运行或至少出现这样。我试着将输出重定向到另一个日志文件:
It does not run or at least it appear so. I've tried redirecting the output to another log file too:
* * * * * /usr/bin/php -q /var/www/myapp/services/myservice.php >> /mylog.log 2>&1
我没有任何迹象表明脚本被运行。我想它不,但我不知道还有什么要寻找。我甚至重新启动了cron守护进程。
Still nothing. I have no indication whatsoever of the script being ran. I guess it doesn't, but I have no idea what else to look for. I even restarted the cron daemon.
我知道在StackOverflow上有类似的问题,但是没有一个答案是我的解决方案。
I know there are similar questions on StackOverflow, but none of the answers turned out to be a solution for me. This is literally driving me crazy, I will greatly appreciate any help.
推荐答案
通常有很多事情你需要可以将可执行文件从命令行移动到cron作业。
There are usually a significant number of things you need to do to move an executable from the command line to a cron job.
默认情况下,cron作业获得一个最小的环境,几乎肯定不会有完整的路径主机的其他环境变量)您的登录会话有。
By default, cron jobs get a minimal environment which will almost certainly not have the full path (and a host of other environment variables) that your login sessions have. You may also not be in the same directory (as you have discovered).
我想做的是执行:
env | sed 's/^/export /' >$HOME/cron.env
以获得完整的环境,然后确保我的cron作业在尝试做真正的工作之前执行该脚本。生成的脚本可能需要少量整理(引用,删除 _
和 PWD
等临时环境变量,因此
from a login session to get the full environment, then make sure that my cron jobs execute that script before attempting to do the real work. The resultant script may need a small amount of tidying up (quoting, removing transient environment variables like _
and PWD
and so forth).
这样我可以确定登录和cron环境是一样的。
That way I can be sure that the login and cron environments are identical.
这篇关于无法获取PHP cron脚本运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!