简体   繁体   English

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

[英]PHP: Require path does not work for cron job?

I have a cron job that needs to include this file: 我有一个需要包含此文件的cron作业:

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

however, when it is run via the cron job (and not my local testing), the relative path does not work. 但是,当它通过cron作业(而不是我的本地测试)运行时,相对路径不起作用。 the cron job runs the following file (on the live server): cron作业运行以下文件(在实时服务器上):

/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 使用与cron作业相同的绝对格式, common.php将位于

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

does that mean i have to replace my line 2 with: 这是否意味着我必须用以下代码替换我的第2行:

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

?

thanks! 谢谢!

Technically seen the php script is run where cron is located; 从技术上看,php脚本是在cron所在的位置运行的; ex. 恩。 If cron was in /bin/cron, then this statement would look for common.php in /bin/includes/common.php. 如果cron在/ bin / cron中,则此语句将在/bin/includes/common.php中查找common.php。

So yeah, you'll probably have to use fullpaths or use set_include_path 所以,是的,您可能必须使用完整路径或使用set_include_path

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

nono. 不,不。 you need to use absolute paths on crons. 你需要在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

With all do respect to all the current answers, they all went to "change the php code" approach. 一切都尊重所有当前的答案,他们都去“改变PHP代码”的方法。

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. 我不喜欢改变我的PHP文件只是为了从cron运行它,因为它降低了代码的可移植性并增加了忘记更改一个或两个相对路径并破坏程序的机会。

Instead change the directory at the cron tab line, and leave all your relative paths and your PHP files untouched. 而是更改cron选项卡行中的目录,并保持所有相对路径和PHP文件不变。 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. 根据PHP脚本中的代码,它可能只在从特定目录调用时才能正确运行。 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: 以下命令显示如何从特定目录调用PHP脚本:

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

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. 如果相对路径不起作用,则表示当cron任务运行时设置的当前目录不是/ home / username123 / public_html。 In such cases, you can only use an absolute path. 在这种情况下,您只能使用绝对路径。

It sounds as simple as just some script you are running is setting the include_path and you are including that script. 听起来就像你正在运行的一些脚本设置include_path一样简单,你要包含那个脚本。 use phpinfo() to check the include_path global vs local setting. 使用phpinfo()检查include_path全局与本地设置。

An alternative to the solutions which recommend absolute path specification is using a chdir in your script. 推荐绝对路径规范的解决方案的替代方案是在脚本中使用chdir 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. 等等。

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

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