无法运行 PHP cron 脚本

2021-12-27 00:00:00 cron command-line php

我有一个需要每分钟运行一次的 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.

推荐答案

我找到了解决我的问题的方法:

I found a solution for my issue:

https://serverfault.com/questions/97828/php-from-command-line-path-problems/97881#97881

事实证明我需要 cd(更改目录)到脚本目录中,然后调用它.令人惊讶,因为我使用的是绝对路径,但它有效.感谢那些花时间回复的人.

It turns out I needed to cd (change directory) into the script dir and then call it. Surprising, since I'm using absolute paths, but it works. Thanks to those who have taken the time to respond.

相关文章