crontab not running as root

5,990

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 :

/home/wayne/scripts/mysql_backup.sh
Share:
5,990

Related videos on Youtube

Wayne Gregori
Author by

Wayne Gregori

Updated on September 18, 2022

Comments

  • Wayne Gregori
    Wayne Gregori over 1 year

    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:

    MyUSER="root" # USERNAME
    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
    DEST="/home/wayne/backup"
    
    # Main directory where backup will be stored
    MBD="$DEST/mysql"
    
    # Get hostname
    HOST="$(hostname)"
    
    # Get data in dd-mm-yyyy format
    NOW="$(date +"%d-%m-%Y")"
    
    # File to store current backup file
    FILE=""
    # Store list of databases
    DBS=""
    
    # DO NOT BACKUP these databases
    IGGY="thisdatabaseschema thatdatabase herdata mydata"     
    [ ! -d $MBD ] && mkdir -p $MBD || :
    
    # Only root can access it!
    $CHOWN 0.0 -R $DEST
    $CHMOD 0600 $DEST
    
    # Get all database list first
    DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
    
    for db in $DBS
    do
    skipdb=-1
    if [ "$IGGY" != "" ];
    then
    for i in $IGGY
    do
    [ "$db" == "$i" ] && skipdb=1 || :
    done
    fi
    
    if [ "$skipdb" == "-1" ] ; then
    FILE="$MBD/$db.$HOST.$NOW.gz"
    # 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
    fi
    done
    

    Thanks, Wayne

    • BlitZz
      BlitZz about 10 years
      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.
    • BlitZz
      BlitZz about 10 years
      I suggest adding a shebang (#!/bin/bash) as the first line of your script.
    • terdon
      terdon about 10 years
      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.
  • Wayne Gregori
    Wayne Gregori about 10 years
    Thanks for the direction... my guess is that it is a path problem. Thank OpenSource community! love you guys/gals