Linux

PHP 的問題包括來自命令行(或 cronjob)

  • August 6, 2014

我正在嘗試在我的 AWS EC2 實例上設置一個 cronjob。crontab 文件中的實際條目沒有任何問題,但是當我嘗試在命令行中執行命令時,我得到了這個響應。

PHP Warning:  include(../scripts/connect.php): failed to open stream: No such file or directory in /var/www/htdocs/crons/emailnotifications.php on line 2
PHP Warning:  include(): Failed opening '../scripts/connect.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/htdocs/crons/emailnotifications.php on line 2
PHP Warning:  include(../scripts/functions.php): failed to open stream: No such file or directory in /var/www/htdocs/crons/emailnotifications.php on line 3
PHP Warning:  include(): Failed opening '../scripts/functions.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/htdocs/crons/emailnotifications.php on line 3
PHP Warning:  strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for 'UTC/0.0/no DST' instead in /var/www/htdocs/crons/emailnotifications.php on line 4
PHP Warning:  mysql_query(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /var/www/htdocs/crons/emailnotifications.php on line 6
PHP Warning:  mysql_query(): A link to the server could not be established in /var/www/htdocs/crons/emailnotifications.php on line 6
PHP Warning:  mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /var/www/htdocs/crons/emailnotifications.php on line 7

我可以在我的瀏覽器中載入此頁面,它工作正常,但不能從命令行(或 cronjob)載入。

包括(../scripts/connect.php)

您的腳本正在使用相對目錄。這些連結到目前工作目錄。當 cron 執行時,你的 CWD 幾乎肯定不是你想像的那樣。在您的 cron 腳本中設置 CWD,或者更新您的 PHP 程式碼以使用相對於它們自身的路徑。可能是什麼include(realpath(dirname(__FILE__).'/../scripts/connect.php'))

幾乎可以肯定,您的其餘問題也與您的 cron 會話的特定環境與 Web 伺服器執行腳本時的環境不同。找出差異,並修復它們。

引用自:https://serverfault.com/questions/618453