0

I have a crontask which runs at 1am every morning and dumps all databases.

0 1 * * * /var/backups/dumpit.sh 2>&1>> /var/backups/dumpit.log

The dumpit.sh source:

#!/bin/bash
/usr/bin/mysqldump -u user -ppass --all-databases | gzip > /var/backups/mysql_`date +%u`.sql.gz

The script runs okay when executed manually but fails when run via the cron. Nothing is added to the dumpit.log file. Do I need to turn on logging in my .myc.nf file? Is there anything else I could try?

xylar
  • 167
  • 8
  • Work your way through this http://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it. If that's the full extent of your crontab (or it's the last line) ensure there is a newline at the end of it. – user9517 Apr 07 '13 at 17:46
  • @Iain, thanks. I have tested the cron and it is working fine. I think it is related to the `cron runs your command in a restricted environment.` section. However I am using absolute paths, and I thought by adding `#!/bin/bash` it would be able to run the `/usr/bin/mysqldump` command. – xylar Apr 07 '13 at 17:59
  • Doesn't the `sql.gz` file get created at all? As it is now, `dumpit.log` will always be empty. If you want to redirect standard error to `dumpit.log` you need to move `2>&1` to the end of the line like this: `>> /var/backups/dumpit.log 2>&1` or simply: `2> /var/backups/dumpit.log`, since `gzip` sends *all* standard output to `mysql_`date +%u`sql.gz`. – jaume Apr 07 '13 at 19:24
  • @jaume `sql.gz` doesn't get created. Do you mean my cron entry should be either: `0 1 * * * /var/backups/dumpit.sh >> /var/backups/dumpit.log 2>&1` or `0 1 * * * /var/backups/dumpit.sh 2> /var/backups/dumpit.log`? – xylar Oct 04 '13 at 10:21
  • 2
    Use `0 1 * * * /var/backups/dumpit.sh 2>> /var/backups/dumpit.log` instead. `>>` appends standard error to the file. Now `/var/backups/dumpit.log` should record any errors and give you a hint of what's going wrong. – jaume Oct 04 '13 at 10:39
  • 1
    Thanks. Error was logged as `/bin/sh: /var/backups/dumpit.sh: Permission denied`. So I updated permissions for `dumpit.sh` to: `-rwxr--r-- 1 root root ` which worked. – xylar Oct 04 '13 at 11:30

0 Answers0