問(wèn)題描述
我有一個(gè)需要每分鐘運(yùn)行一次的 PHP 腳本.我已經(jīng)確保腳本可以從命令行運(yùn)行,并且我使用絕對(duì)路徑來(lái)避免任何環(huán)境問(wèn)題:
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 用戶身份手動(dòng)運(yùn)行它可以正常工作,正如我從腳本寫(xiě)入的日志文件中看到的那樣.可以肯定的是,該腳本也具有執(zhí)行權(quán)限.
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.
但是,當(dāng)在 cron 中放置相同的命令時(shí):
However, when placing the same exact command in a cron:
* * * * * /usr/bin/php -q /var/www/myapp/services/myservice.php
它沒(méi)有運(yùn)行,或者至少看起來(lái)是這樣.我也嘗試將輸出重定向到另一個(gè)日志文件:
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
還是沒(méi)有.我沒(méi)有任何跡象表明正在運(yùn)行的腳本.我想它沒(méi)有,但我不知道還要尋找什么.我什至重新啟動(dòng)了 cron 守護(hù)進(jìn)程.
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 上也有類(lèi)似的問(wèn)題,但沒(méi)有一個(gè)答案對(duì)我來(lái)說(shuō)是一個(gè)解決方案.這簡(jiǎn)直讓我發(fā)瘋了,我將不勝感激任何幫助.
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.
推薦答案
我找到了解決我的問(wèn)題的方法:
I found a solution for my issue:
https://serverfault.com/questions/97828/php-from-command-line-path-problems/97881#97881
事實(shí)證明我需要 cd(更改目錄)到腳本目錄中,然后調(diào)用它.令人驚訝,因?yàn)槲沂褂玫氖墙^對(duì)路徑,但它有效.感謝那些花時(shí)間回復(fù)的人.
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.
這篇關(guān)于無(wú)法運(yùn)行 PHP cron 腳本的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!