2013-06-04 152 views
0

我有一个脚本,我们称之为mainfile.php,它包含一个包含在同一目录中的文件的mainfile.php。我包括它,如下所示:通过cron作业执行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 

任何想法?

+0

运行此命令时由cron,你必须总是指定脚本中的完整路径运行的任务。 – k102

回答

2

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

更改require_once到:

require_once __DIR__ . '/myincludefile.php'; 

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

+0

您的解决方案有效 – EastsideDeveloper

0

请更改路径,以绝对路径require_once './myincludefile.php';

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