問題描述
我有一個需要每分鐘運行一次的 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.
這篇關于無法運行 PHP cron 腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!