Belgin Fish
Belgin Fish

Reputation: 19827

Crontab not running PHP scripts properly

I have the following commands set in my crontab

0 * * * * root cd /home/domain/public_html/webcrawler && php cron1.php
40 * * * * root cd /home/domain/public_html/webcrawler && php cron2.php

The thing is they are not running. Is there any noticeable errors in the commands I have set? The php scripts run fine when run from ssh, or loaded in the browser. I have tried with and without root at the start.

Upvotes: 1

Views: 137

Answers (2)

Will B.
Will B.

Reputation: 18416

Change out the && with ; to read like

0 * * * * root cd /home/domain/public_html/webcrawler; php cron1.php

or execute it from the absolute path

0 * * * * root php /home/domain/public_html/webcrawler/cron1.php

When using the absolute path you can force your cron script's current working directory by performing

chdir(__DIR__); //or chdir(dirname(__FILE__));

You can also add the PHP shebang to the top of you cron.php file (your php binary may have a different path)

#!/usr/bin/php

http://php.net/manual/en/features.commandline.usage.php

Then you can execute the script without needing to reference the php binary location using

0 * * * * root /home/domain/public_html/webcrawler/cron1.php

Upvotes: 2

Machavity
Machavity

Reputation: 31614

You should call PHP cron jobs with the full path (note, your PHP may be in a different location)

0 * * * * /usr/bin/php /path/to/your/script.php

Upvotes: 4

Related Questions