参见英文答案 > Why crontab scripts are not working? 46个
我有以下bash脚本:
#!/bin/bash
mysqldump -u ******** -p******** --all-databases | gzip > /home/srvlinux01/MySQLBackups/database_$(date +\%Y-\%m-\%d).sql.gz
它位于/ home / srvlinux01 / MySQLBackups / as backup.sh,具有以下权限
-rwxr--r-- 1 root root 134 feb 27 12:48 backup.sh
我已经在sudo crontab -e上设置了一个cronjob,以便每天晚上运行它
#Automatic MySQL backup
30 3 * * * sh /home/srvlinux01/MySQLBackups/backup.sh
但我收到通过电子邮件发送以下错误:
sh: 0: Can't open /home/srvlinux01/MySQLBackups/backup.sh
我一直在尝试不同的设置,但无法弄清楚出了什么问题.我可以手动运行脚本,一切都很顺利,所以我猜我的cronjob条目有问题,但不能真正理解是什么.你能帮帮我解决一下吗?谢谢!
解决方法:
你需要给你的cron一个PATH.例如:
SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin
在您的情况下,尝试将此命令放在您的命令之前.有关为什么需要PATH变量的更多信息,请查看this question中的社区wiki.这是一段摘录;基本上这个想法是cron不读取/ etc / environment:
A common “gotcha” is the PATH environment variable being different. Maybe your cron script uses the command
somecommand
found in/opt/someApp/bin
, which you’ve added toPATH
in/etc/environment
? cron does not read that file, so runningsomecommand
from your script will fail when run with cron, but work when run in a terminal. To get around that, just set your ownPATH
variable at the top of the script.