从浏览器运行时,php脚本require()语句获取文件,而从Plesk CRON作业运行时,则不获取文件

时间:2019-02-10 13:58:56

标签: php linux cron scheduled-tasks plesk

我有一个任务脚本,如果我通过从浏览器访问php文件来运行它,则该脚本可以很好地工作。但是,当我尝试通过Plesk Task Scheduler运行它时,它失败并出现致命错误,无法加载require()文件。

require语句是一个简单的相对路径:

require('../../../app.config.php');

错误是:

PHP Fatal error: require(): Failed opening required '../../../app.config.php'

我认为这可能与include_path有关,但是我对此并不了解,因此在那一点上有些失落。

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:1)

相对路径由当前目录解析。解决方案:获取脚本目录并结合相对路径,例如:

async function createTopic(topicName: string): Promise<any> {
  const topicInstance = pubsubClient.topic(topicName);
  const [exists] = await topicInstance.exists();
  if (exists) {
    logger.info(`${topicName} topic is existed`);
    return;
  }
  return pubsubClient
    .createTopic(topicName)
    .then((data) => {
      logger.info(`Create topic ${topicName} successfully`);
      return data;
    })
    .catch((err) => logger.error(err));
}

答案 1 :(得分:0)

您需要使用绝对路径。如果直接调用一个脚本或该脚本包含在另一个脚本中,则可能会产生不同的相对路径。

例如:

a.php is on path A/B/C the ../../ will result in in A/
b.php is on path A/B/C/D, if it inlcudes the a.php then the ../../ on a.php will result in A/B/.