Author Topic: Cron jobs containing relative paths fail  (Read 9265 times)

0 Members and 1 Guest are viewing this topic.

Offline
*
Cron jobs containing relative paths fail
« on: September 18, 2017, 01:18:39 PM »
It seems, if a PHP script contains something like:

Code: [Select]
include('../another-file.php');
It will work properly if opened in browser or CLI, but fail in cron with a message similar to:

Code: [Select]
PHP Warning:
include(../another-file.php): failed to open stream: No such file or directory in /home/account/public_html/cron-test.php on line 3

Scripts can be modified to use absolute paths instead of relative paths, but that's not the best solution for everyone, especially if lots of files are executed using cron jobs.

Is there a fix for this at the control panel (CWP) level?

Offline
*
Re: Cron jobs containing relative paths fail
« Reply #1 on: September 18, 2017, 09:41:11 PM »
An example of a fix (not tested thoroughly) could be to include this line in the beginning of each script that is executed via cron.

Code: [Select]
chdir(__DIR__);
Still, would be nice to see a fix on CWP level.

Offline
*****
Re: Cron jobs containing relative paths fail
« Reply #2 on: September 21, 2017, 04:30:27 PM »
use :
Code: [Select]
include($_SERVER["DOCUMENT_ROOT"] . "patch to another-file.php");
or

Code: [Select]
include(dirname(__FILE__) . "path to another-file.php");
« Last Edit: September 21, 2017, 04:32:43 PM by Sandeep »

Offline
*
Re: Cron jobs containing relative paths fail
« Reply #3 on: September 23, 2017, 03:40:47 AM »
OK, this works for files in the same folder by supplying the absolute URL, but what about relative paths?

What's the replacement for ('../abc.php')?

How about ('../../abc.php')?

Or ('./abc.php')?