2

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 backup.php.
  • It generates a backup (sql within a gz archive) and stores it in folder /backup.
  • It adds a few lines of text to /backup/log.txt.

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:

  • Running the script directly in the console using php backup.php
  • Running cron job: * * * * * php -v > /var/www/cmdb/backup/php-v.txt
  • Running cron job: * * * * * php -f /var/www/cmdb/test.php > /var/www/cmdb/backup/text.txt (writing a string to a file)

I think these tests indicate that:

  • backup.php script works without error
  • Cron is running
  • The backup folder is writeable
  • PHP command is accessible from crontab

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

7
  • Hi Paul, do you have some outputs (echo, print, print_r, etc.) for debug purposes in your code? It may stop your code executing when launched as planned task. Commented Jun 3, 2016 at 7:50
  • 1
    Did you check whether the php script is executable? What are the logs saying? What is in the script? Remember the paths in the script must be full, not relative. Commented Jun 3, 2016 at 8:06
  • Without any logging/error output there's not a great deal we can do to help. One obvious thing is what user is the PHP script running as when run from cron? Does it have the permissions needed to do its job? Commented Jun 3, 2016 at 8:07
  • 1
    You could * * * * * php -f /var/www/cmdb/backup.php > /tmp/cronlog 2>&1 and perhaps see the output form the script Commented Jun 3, 2016 at 8:11
  • Thank you all for the good comments and advice, I tried all. @Oli: you helped me most, because now indeed I get some leads. "file_put_contents(sql, gz or log) failed to open stream: No such file or directory in (...)". Must be rights issues. Which I don't understand, as I can run the script on the command line as any user I want (not just root) and the files get created just fine. Commented Jun 3, 2016 at 8:53

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.