anup kattel
anup kattel

Reputation: 1

php path, cron and cpanel

I've spent days still can't figure out this.

I have following file structure under public_html:

cron_jobs/file.php contains - > include('../base/basefile.php')
base/basefile.php contains - > include('baseSubFile.php')

when I run

/pathtophp/php -f ~/public_html/cron_jobs/file.php

it works ok but when I copy the same command to cron in cpanel, I get error saying

'basesubfile.php' can't be found

Please help.

Upvotes: 0

Views: 676

Answers (3)

Billy
Billy

Reputation: 788

Simply put this at the top of your PHP script:

chdir(dirname(__FILE__));

Upvotes: 0

Luc M
Luc M

Reputation: 17314

You should used

include dirname( __FILE__ ) . '/../base/basefile.php';

and

include dirname( __FILE__ ) . '/baseSubFile.php';

The function dirname returns parent directory's path

Upvotes: 1

onteria_
onteria_

Reputation: 70517

Cron won't run from the same directory as your php file is in, so you'll need to change to it first:

cd /home/user/public_html/cron_jobs/ && /pathtophp/php -f file.php

I recommend the full path versus ~ when dealing with cron scripts to avoid confusion

Upvotes: 2

Related Questions