Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysqldump creates empty file when run via cron on linux

I have a bash script mysql_cron.sh that runs mysqldump

#!/bin/bash
/usr/local/mysql/bin/mysqldump -ujoe -ppassword > /tmp/somefile

This works fine. I then call it from cron:

20 * * * * /home/joe/mysql_cron.sh

and this creates the file /tmp/somefile, but the file is always empty. I have tried adding a

source /home/joe/.bash_profile 

to the script to make sure cron has the right env variables, but that doesn't help. I see many other people having this problem but have found no solution. I've also tried the '>' operator in the crontab to cat any cron errors to a file, but that doesn't seem to generate any errors. Any troubleshooting ideas welcomed. Thanks!

like image 246
bethesdaboys Avatar asked Feb 14 '11 18:02

bethesdaboys


2 Answers

Add output of error information to file (as Damp has said), so that you can check if there is any error:

#!/bin/bash
/usr/local/mysql/bin/mysqldump -ujoe -ppassword > /tmp/somefile 2>&1

You can also take a look at MySQL's log files at /var/log in case there is some hint there.

like image 158
davidag Avatar answered Nov 14 '22 09:11

davidag


Add this line to your script and compare the result between running it from cron versus running it directly:

env > /tmp/env.$$.out

The $$ will be replaced in the resulting filename by the PID of the parent process (cron or the shell). You should be able to diff the two files and see if anything significant is different between the two environments.

like image 44
Dennis Williamson Avatar answered Nov 14 '22 08:11

Dennis Williamson