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!
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.
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 .
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.
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With