Mysql backup scripts not running as root crontab - help

I'm trying to run a crontab as root but the script is not running successfully. I can see in syslog that the script is executing but it does not finish.

Mar 28 04:01:01 ubuntumaster CRON[16812]: (root) CMD home/wayne/scripts/mysql_backup.sh)

Running the script at the command line works great... here's the script:

MyPASS="devan13" # PASSWORD
MyHOST="localhost" # Hostname

# Linux bin paths, change this if it can not be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"

# Backup Dest directory, change this if you have someother location

# Main directory where backup will be stored

# Get hostname

# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"

# File to store current backup file
# Store list of databases

# DO NOT BACKUP these databases
IGGY="thisdatabaseschema thatdatabase herdata mydata"     
[ ! -d $MBD ] && mkdir -p $MBD || :

# Only root can access it!

# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"

for db in $DBS
if [ "$IGGY" != "" ];
for i in $IGGY
[ "$db" == "$i" ] && skipdb=1 || :

if [ "$skipdb" == "-1" ] ; then
# do all inone job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir :)
$MYSQLDUMP --quick -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE

Thanks, Wayne

  • 2
    Could you share also the crontab definition you've done ? – Benoit Mar 28 '14 at 14:09
  • 3
    I hope your mysql password is not actually 'devan13'. If so, you'll probably want to change it now that you've posted it in a public forum. – roadmr Mar 28 '14 at 14:26
  • 2
    I suggest adding a shebang (#!/bin/bash) as the first line of your script. – roadmr Mar 28 '14 at 14:27
  • Whenever you need to debug things like this, you should add echo statements at different points in your script, then in the crontab redirect output to a log file script.sh > /path/to/log 2>&1 that way you can find where it is getting stuck. – terdon Mar 28 '14 at 14:35

Unless you've skipped some characters when writting the syslog output in your question, it looks like you've made a typo in your CRON command by forgetting the / in front of the command that should looks like :


Ensure to run:

sudo crontab -e
(or crontab -e as root)

And add the desired script:

*/15 * * * * /bin/bash /home/wayne/scripts/mysql_backup.sh

This will initiate the cron every 15 minutes, obviously you may want to change that.


My money's on Benoit for having the truly correct answer here, but as soon as you can verify that your script executes you will likely find some additional errors.

In short, make sure you also check your environment! You're half way there by using variables that define the location of the commands (MYSQL="$(which mysql)") but which mysql just reads your PATH to determine its output. which in a script like this doesn't do anything, you might as well just write mysql directly when the command is used, or hardcode the location for mysql in the variable instead of using which to try and populate it for you.

On a test Ubuntu x64 12.04.3 install, * * * * * env > test.out in a crontab shows that the PATH is /usr/bin:/bin. If one of your commands does not exist within that PATH, then it will not execute properly!

As a final suggestion, add set -x to the top of your bash script (assuming it is bash) to debug what the script is actually doing.

  • Thanks for the direction... my guess is that it is a path problem. Thank OpenSource community! love you guys/gals – Wayne Gregori Mar 30 '14 at 0:55

Problem solved. The cron job was running each night but not completing. The problem was with the script. The cron shell did not interpret "==" as equal, i.e. in the cron shell "1==1" was evaluated as false. However, when I ran the program at the command line it worked. I read somewhere that the bash shell understands == or = to mean equal... but the bourne shell does not interpret "==" as equal... I had to replace my "==" with "=" and it worked fine. Thanks for the help. Wayne


i use this script and call it a day "easy cronjob handler"


  • 1
    This may be a useful tool, but your post does not answer the question. – Jos Apr 25 '14 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.