Volatil3
Volatil3

Reputation: 14978

Cron not executing Shell Script on Ubuntu

I have a remote .sh script to take backup of mySQL file. Cron is running by .sh file not executing. Content of Crontab are:

#SHELL=/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin 
* * * * * /home/ubuntu/dscript/db_backup.sh > /tmp/db.output

File Contents are:

#!/bin/bash

DATE=$(date +%Y%m%d_%H%M)

zipname="db_prod_$DATE.sql"
mysqldump db_prod > $zipname
aws s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname

zipname="db_staging_$DATE.sql"
mysqldump db_staging > $zipname
aws s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname

Bash path

bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

I am on Ubuntu.

File is is in +x mode.

Upvotes: 0

Views: 233

Answers (2)

Ephemeral
Ephemeral

Reputation: 423

#!/usr/bin/bash

DATE=$(date +%Y%m%d_%H%M)
zipname="db_prod_$DATE.sql"
SCRIPT_LOG_FILE="/tmp/backup.log"

echo "[${DATE}] [INFO] Starting personnal backup" > ${SCRIPT_LOG_FILE}
$(which mysqldump) db_prod > $zipname
if [ "$?" -eq "0" ] && [ -f ${zipname} ];then
    echo "[$(date +%Y%m%d_%H%M)] [INFO] Mysqldump save file ${zipname}" > ${SCRIPT_LOG_FILE}
    # file is created continue job with this file ...
    $(which aws) s3 mv $zipname s3://mybackups/$zipname
    rm -rf $zipname
else
    echo "[$(date +%Y%m%d_%H%M)] [ERROR] Mysqldump can't save file ${zipname}" > ${SCRIPT_LOG_FILE} 
fi
exit 0

And crontab -l must be:

* * * * * /home/ubuntu/dscript/db_backup.sh &> /tmp/error.and.result.db.output 

Upvotes: 1

Ephemeral
Ephemeral

Reputation: 423

check cron error: try to see error with:

~$ sudo cat /var/log/syslog|grep CRON|grep db_backup.sh
~$ sudo cat /var/log/syslog|grep CRON|grep MY_USERNAME

(Permission denied or other)

where MY_USERNAME is the user execute : crontab -e

check script error:

* * * * * /home/ubuntu/dscript/db_backup.sh > /tmp/result.db.output
* * * * * /home/ubuntu/dscript/db_backup.sh 2> /tmp/error.db.output

or simply

* * * * * /home/ubuntu/dscript/db_backup.sh &> /tmp/error.and.result.db.output 

The question is whether the error comes from the execution of cron or is in the script, the above methods will help you find the source of the problem.

Upvotes: 0

Related Questions