Cron没有在Ubuntu上执行Shell脚本

时间:2015-07-31 06:07:34

标签: cron crontab

我有一个远程.sh脚本来备份mySQL文件。 Cron正在运行.sh文件未执行。 Crontab的内容是:

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

文件内容为:

#!/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路径

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

我在Ubuntu上。

文件处于+x模式。

2 个答案:

答案 0 :(得分:1)

#!/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

crontab -l <​​/ strong>必须是:

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

答案 1 :(得分:0)

检查cron错误: 尝试查看错误:

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

(许可被拒绝或其他)

其中MY_USERNAME是用户执行: crontab -e

检查脚本错误:

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

或只是

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

问题是错误是来自执行cron还是在脚本中,上述方法将帮助您找到问题的根源。