PHP:需要路径不适用于 cron 作业?

2022-01-03 00:00:00 absolute-path cron include php require

我有一个需要包含此文件的 cron 作业:

I have a cron job that needs to include this file:

require '../includes/common.php';

但是,当它通过 cron 作业(而不是我的本地测试)运行时,相对路径不起作用.cron 作业运行以下文件(在实时服务器上):

however, when it is run via the cron job (and not my local testing), the relative path does not work. the cron job runs the following file (on the live server):

/home/username123/public_html/cron/mycronjob.php

这是错误:

Fatal error: require(): Failed opening required '../includes/common.php' 
(include_path='.:/usr/lib/php:/usr/local/lib/php') in 
/home/username123/public_html/cron/mycronjob.php on line 2

使用与 cron 作业相同的绝对格式,common.php 将位于

using the same absolute format as the cron job, common.php would be located at

/home/username123/public_html/includes/common.php

这是否意味着我必须将第 2 行替换为:

does that mean i have to replace my line 2 with:

require '/home/username123/public_html/includes/common.php';

?

谢谢!

推荐答案

从技术上看php脚本是在cron所在的地方运行的;前任.如果 cron 在/bin/cron 中,则该语句将在/bin/includes/common.php 中查找 common.php.

Technically seen the php script is run where cron is located; ex. If cron was in /bin/cron, then this statement would look for common.php in /bin/includes/common.php.

是的,您可能必须使用完整路径或使用 set_include_path

So yeah, you'll probably have to use fullpaths or use set_include_path

set_include_path('/home/username123/public_html/includes/');
require 'common.php';

相关文章