命令行(或 cronjob)中 PHP 包含的问题

命令行(或 cronjob)中 PHP 包含的问题

我正在尝试在我的 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)加载。

答案1

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

您的脚本使用的是相对目录。这些目录链接到当前工作目录。当 cron 运行时,您的 CWD 几乎肯定不是您想象的那样。要么在您的 cron 脚本中设置 CWD,要么更新您的 PHP 代码以使用相对于自身的路径。也许像include(realpath(dirname(__FILE__).'/../scripts/connect.php'))

您的其余问题也几乎肯定与您的 cron 会话的特定环境与 Web 服务器运行脚本时的环境不同有关。找出差异并修复它们。

相关内容