PHP: Require path does not work for cron job? – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and include . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about PHP: Require path does not work for cron job?.
I have a cron job that needs to include this file:
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):
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
does that mean i have to replace my line 2 with:
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';
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
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
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.
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.
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.