Paul Maclean
Paul Maclean

Reputation: 631

PHP script does not run as cron job on Ubuntu Server

I've written a php script that creates a backup of a mysql database and set it to run daily using crontab on Ubuntu server, but the script is never executed (at least so it seems).

The script is called in crontab for root user like this (sudo crontab -e):

* * * * * php -f /var/www/cmdb/backup.php

(Time needs to be adjusted, but this is for testing purposes.)

Nothing happens. No file gets created, no log gets modified.

However, all of the following tests succeed:

I think these tests indicate that:

So, why isn't backup.php doing anything when run from crontab?

Upvotes: 2

Views: 590

Answers (2)

fedorqui
fedorqui

Reputation: 289885

As seen in the comments, the problem lied in the paths within your script: you wanted to use /var/www/cmdb/backup and just wrote backup because you assumed it was fine since the script runs in the same directory.

So, in general, never assume paths in your scripts from crontab: cron runs under a very limited environment and its running path is normally /root or just /. So whatever you have in your script make it use the full path.

Or, otherwise, move to the path and then run the script:

* * * * * cd /var/www/cmdb/ && php -f backup.php

Upvotes: 1

Gravy
Gravy

Reputation: 12445

Could you try running the command directly from the command line to check output?

php -f /var/www/cmdb/backup.php

Could you try being explicit with the location of php?

* * * * * /usr/bin/php -f /var/www/cmdb/backup.php

Upvotes: 0

Related Questions