shell script does not work in crontab
写了一个备份数据库的脚本,之前测试通过,最近在另一个服务器上照搬,结果定时执行总是不行,但手动执行没问题。今天花时间解决一下。
先从crontab入手:添加bash/sh xxx 2> /tmp/crontab_script_log.txt 2>&4 从日志中发现 which docker-compose 可以执行,但是 docker-compose verison报错: docker-compose-entrypoint.sh: exec: line 20: verison: not found
原来是之前使用二进制的docker-compose,而新服务器使用docker的方式安装的docker-compose(其实就是一个shell脚本运行一个docker-compose的容器)估计问题出在脚本中的脚本调用问题。切换为二进制安装,完美解决问题。以后不用容器方式按照docker-compose了。
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
#之前的方式 INSTALL AS A CONTAINER
sudo curl -L --fail https://github.com/docker/compose/releases/download/1.27.4/run.sh -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
#备份到本机脚本
#!/bin/bash
FILENAME=siteName.$(date '+%Y%m%d%H%M%S').db.backup.sql
cd /var/www/html/siteName
docker-compose -f docker-compose.yml.staging exec -T db mysqldump -uroot -proot joomla > /tmp/$FILENAME \
&& cd /tmp/ \
&& tar zcvf $FILENAME.tar.gz $FILENAME \
&& rm $FILENAME -rf
- https://unix.stackexchange.com/questions/339771/how-to-prove-that-the-interpreter-is-bin-bash-when-called-from-cron 奇怪的引号
- 魔法PATH
- https://stackoverflow.com/questions/20582224/shell-script-not-running-via-crontab-but-runs-fine-manually
- https://www.digitalocean.com/community/questions/why-is-cron-not-running-my-sh-script
- https://unix.stackexchange.com/questions/185236/bashscript-works-from-terminal-but-not-from-crontab
- https://github.com/docker-library/postgres/issues/296 docker-compose-entrypoint.sh: exec: line 20: verison: not found