Asked  7 Months ago    Answers:  5   Viewed   38 times

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!

 Answers

16

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';
Wednesday, March 31, 2021
 
EnTee
answered 7 Months ago
95

If you do require('../includes/common.php'), the path is traversed relative to the current working directory.

If you do require('common.php'), the file is searched in the include path, and in the directory of the script which calls the require().

To solve this, first change directory in your crontab:

cd /home/fini7463/public_html; php -f cronjob.php
Wednesday, March 31, 2021
 
RompelStompel
answered 7 Months ago
93

Don't rely on PATH being set for cronjob through .bash_profile (it's a shell feature and cronjobs are not running through a shell), you should rather use something like

* * * * * /Applications/MAMP/bin/php/php5.4.4/bin/php-something? /path/to/vendor/laravel/framework/src/Illuminate/Foundation/start.php

A test could be something like this:

# /tmp/test.php
<?php file_put_contents('/tmp/a_test', `id`."n".var_export($_ENV, true)."n".var_export(extension_loaded('mcrypt'), true));
# in crontab
* * * * * /Applications/MAMP/bin/php/php5.4.4/bin/php-something? /tmp/test.php

Run php /tmp/test.php one time manually to spot the differences between you running the script, and cron; and always try to use absolute paths in crontab (in this case to your php binary).

Friday, May 28, 2021
 
Null
answered 5 Months ago
69

It turned out to be an authentication problem. I solved it by adding the following two lines to the CURL configuration:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

Hope it helps someone.

Friday, May 28, 2021
 
Saxophlutist
answered 5 Months ago
84

You need to add the after the directory name:

include(__DIR__ . "\..\another_folder\file_2.php");

This will make the path be

C:xampphtdocsmain_folder..another_folderfile_2.php

instead of

C:xampphtdocsmain_folder..another_folderfile_2.php

Also, for portability, it is advisable to use / instead of , which works on all platforms, including Windows:

include(__DIR__ . "/../another_folder/file_2.php");
Saturday, May 29, 2021
 
MannfromReno
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :