繁体   English   中英

PHP脚本通过cron作业执行时的不同行为

[英]Different behavior when PHP script executed through cron job

我有一个脚本,我们称之为mainfile.php,其中包含一个包含在同一目录中的文件。 我把它包括如下:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once './myincludefile.php';
?>

如果我执行以下操作:

nohup php mainfile.php >/dev/null

它运行没有任何问题。 但是,如果将其转换为cron作业,我会收到以下消息(在我的邮件假脱机中):

PHP Warning:  require_once(./myincludefile.php): failed to open stream: No such file or directory in /home/code/mainfile.php on line 5
PHP Fatal error:  require_once(): Failed opening required './myincludefile.php' (include_path='.:/usr/share/pear:/usr/share/php') in/home/code/mainfile.php on line 5

有任何想法吗?

那是因为当通过cron调用时,脚本的当前工作目录不一样。 (我假设它是/ ,根目录)

require_once更改为:

require_once __DIR__ . '/myincludefile.php';

__DIR__将始终包含当前脚本所在的目录 - 而不是当前工作目录。 这就是为什么建立相对路径很方便。

请在require_once './myincludefile.php';路径更改为绝对路径require_once './myincludefile.php';

并运行此命令

nohup php-q mainfile.php >/dev/null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM