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

发布于 2024-09-07 05:00:22 字数 730 浏览 7 评论 0 原文

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

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

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

/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 将位于

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

这是否意味着我必须替换我的第 2 行

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

谢谢!

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

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

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

and here's the error:

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

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

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

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

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

?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

并安 2024-09-14 05:00:22

在尊重所有当前答案的情况下,他们都采取了“更改 php 代码”的方法。

我不喜欢仅仅为了从 cron 运行 PHP 文件而更改它,因为这会降低代码的可移植性,并增加忘记更改一两个相对路径并破坏程序的机会。

相反,请在 cron 选项卡行更改目录,并保留所有相对路径和 PHP 文件不变。 例如,

1 1 * * * cd /home/username/public_html/&& php -f script.php

检查 这个答案

还要检查这篇文章,我会引用相关部分

根据 PHP 脚本中的代码,它可能仅在从特定目录调用时才能正确运行。例如,如果脚本使用相对路径来包含文件,则仅当从正确的目录调用它时才会运行。以下命令显示如何从特定目录调用 PHP 脚本:

cd /home/username/public_html/; php -q script.php

With all do respect to all the current answers, they all went to "change the php code" approach.

I don't like to change my PHP files just to run it from a cron because it reduces the code portability and increases the chances to forget to change one or two relative paths and break the program.

Instead change the directory at the cron tab line, and leave all your relative paths and your PHP files untouched. For example

1 1 * * * cd /home/username/public_html/&& php -f script.php

check this answer

also check this article, I will quote the relative part

Depending on the code in your PHP script, it may only run correctly when called from a specific directory. For example, if the script uses relative paths to include files, it will only run if it is called from the correct directory. The following command shows how to call a PHP script from a specific directory:

cd /home/username/public_html/; php -q script.php
夏至、离别 2024-09-14 05:00:22

从技术上讲,php 脚本是在 cron 所在的位置运行的;前任。如果 cron 位于 /bin/cron 中,则此语句将在 /bin/includes/common.php 中查找 common.php。

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

set_include_path('/home/username123/public_html/includes/');
require '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.

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';
信愁 2024-09-14 05:00:22

诺诺。您需要在 crons 上使用绝对路径。

我所做的是:

// supouse your cron is on app/cron and your lib is on app/lib
$base = dirname(dirname(__FILE__)); // now $base contains "app"

include_once $base . '/lib/db.inc';

// move on

nono. you need to use absolute paths on crons.

what I do is:

// supouse your cron is on app/cron and your lib is on app/lib
$base = dirname(dirname(__FILE__)); // now $base contains "app"

include_once $base . '/lib/db.inc';

// move on
戒ㄋ 2024-09-14 05:00:22

建议绝对路径规范的解决方案的替代方案是在脚本中使用 chdir。这样,您的相对路径将按预期工作。

例如,切换到脚本的目录:

$curr_dir = dirname(__FILE__);
chdir($curr_dir);

切换到脚本的父目录:

$curr_dir = dirname(__FILE__);
chdir($curr_dir . "/..");

等等。

An alternative to the solutions which recommend absolute path specification is using a chdir in your script. That way, your relative paths will work as expected.

For example, to change to the directory of the script:

$curr_dir = dirname(__FILE__);
chdir($curr_dir);

To change to the parent directory of the script:

$curr_dir = dirname(__FILE__);
chdir($curr_dir . "/..");

And so forth.

执手闯天涯 2024-09-14 05:00:22

如果相对路径不起作用,则说明 cron 任务运行时设置的当前目录不是 /home/username123/public_html。在这种情况下,您只能使用绝对路径。

If the relative path doesn't work, then it means that the current directory set when the cron tasks are running is not /home/username123/public_html. In such cases, you can only use an absolute path.

|煩躁 2024-09-14 05:00:22

这听起来很简单,就像您正在运行的某个脚本正在设置 include_path 并且您正在包含该脚本一样。使用 phpinfo() 检查 include_path 全局与本地设置。

It sounds as simple as just some script you are running is setting the include_path and you are including that script. use phpinfo() to check the include_path global vs local setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文