Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Require path does not work for cron job?

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!

like image 372
gsquare567 Avatar asked Jun 25 '10 14:06

gsquare567


People also ask

Does crontab require full path?

Generally speaking it's considered good practice to always use the full path in scripts to be run from cron, as cron may well have a different setting for PATH to you. The alternative is to call your script from cron as /full/location/of/script , and set a new value for PATH in the script.

Why cron job is not working?

You might discover that your job works when run via the command line, but won't run via cron. This may be due to the location you're executing from or the PATH you're using. The use of relative paths is a common cause of cron issues. In cron, the PATH is set by default to /bin:/usr/bin .

What path does cron use?

The cron daemon automatically sets several environment variables . The default path is set to PATH=/usr/bin:/bin . If the command you are executing is not present in the cron specified path, you can either use the absolute path to the command or change the cron $PATH variable.


1 Answers

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'; 
like image 176
Robus Avatar answered Oct 03 '22 13:10

Robus