Cronjob not running php script

April 28, 2015 12k views
PHP Apache LAMP Stack Linux Commands Ubuntu

I set up cronjob with following command:

sudo crontab -e

I tried following statements for cronjob but it doesn't work:

30 2 * * * php -q /path/to/php/script.php
30 2 * * * /usr/bin/php /path/to/php/script.php
30 2 * * * /usr/bin/php -q /path/to/php/script.php

But when I tried the GET method as follows, it works:

30 2 * * * wget

I have a LAMP setup Ubuntu 12.04 (64-bit) system with my PHP version manually upgraded from 5.3 - 5.4 to run modules on my WHMCS installation.

Any idea what's going wrong while running with PHP mode? I don't want to use GET method!

3 Answers

Crontab executes the task using the current user that ran crontab -e. When you use wget it's handled by Apache using the www-data user/group pair.

First, make sure the script works as expected.

 $ php /path/to/php/script.php

Second, edit the crontab for the Apache user account

 $ sudo crontab -u www-data -e

Now add the crontab and output to a log file.

30 2 * * * php /path/to/php/script.php >> /var/log/apache2/crontab.log

I would recommend that you create a bash script to run this, and then call that with crontab.

Most likely, it is looking for config scripts in a relative path.

Think of this example:

php /path/to/file.php

Now, if the file has something like this:

include '';

This will fail, because you are in your home directory, not in the same directory where resides.

Now, when you call it via a curl/wget call:


Apache will tell it that it is being executed in /path/to/, so when it looks for, it will find it.

Relative paths are good, but only if you fully understand how they work. If you prefer not to use wget/curl, change the cron to something like this:

30 2 * * * cd /path/to/php/; /usr/bin/php -q script.php
Have another answer? Share your knowledge.