运行 crontab 启动脚本困难

运行 crontab 启动脚本困难

我有以下 crontab 作业要在重启时运行:

PATH=/snap/bin:/usr/bin
@reboot run_me_startup.sh >> $HOME/startup_run_log.txt 2>&1

我的 docker 安装已将文件放在 /snap/bin 中(因此我的 PATH 语句)。我的 run_me_startup.sh 脚本如下:

echo "Startup Script"
sleep 240
echo "Running..."
sudo service apache2 stop && sudo service nginx stop
sudo chown $(whoami):$(whoami) /var/run/docker.sock
/snap/bin/docker network create dbnet
/snap/bin/docker network create nginx_network
export aws_access_key_id="secret"
export aws_secret_access_key="secret"
cd $HOME/The6ix/The6ixDjango && pwd && /snap/bin/docker-compose -f docker-compose.prod.yml down --remove-orphans
{
{
rm -rf $HOME/The6ix/The6ixDjango && cd $HOME/The6ix && git clone https://[email protected]/cooneycw/The6ixDjango.git 
} ||
{
cd $HOME/The6ix && git clone https://[email protected]/cooneycw/The6ixDjango.git
}
}
ls
cp $HOME/database.env $HOME/The6ix/The6ixDjango/database.env

我插入了sleep 240,确保代码执行前docker服务已经启动,我的输出日志如下:

Startup Script
Failed to stop nginx.service: Unit nginx.service not loaded.
chown: cannot access '/var/run/docker.sock': No such file or directory
/home/ubuntu/run_me_startup.sh: 4: docker: not found
/home/ubuntu/run_me_startup.sh: 5: docker: not found
/home/ubuntu/The6ix/The6ixDjango
/home/ubuntu/run_me_startup.sh: 8: docker-compose: not found
Cloning into 'The6ixDjango'...
The6ixDjango
/home/ubuntu/run_me_startup.sh: 19: docker-compose: not found
/home/ubuntu/run_me_startup.sh: 20: docker-compose: not found
/home/ubuntu/run_me_startup.sh: 21: docker-compose: not found
/home/ubuntu/run_me_startup.sh: 22: docker-compose: not found
/bin/sh: 1: run_me_startup.sh: not found
/bin/sh: 1: run_me_startup.sh: not found
/bin/sh: 1: run_me_startup.sh: not found
/bin/sh: 1: run_me_startup.sh: not found
/bin/sh: 1: run_me_startup: not found
/bin/sh: 1: run_me_startup: not found
/bin/sh: 1: run_me_startup.sh: not found

我不明白为什么我的 crontab 中的 PATH 语句会出现 docker: not found 错误。我应该担心日志底部的 /bin/sh 注释吗?我是否以某种方式触发了脚本运行两次?Linux 新手……感谢您的慷慨!

答案1

事实证明,我没有意识到我的日志是附加的(我以为每次都会重建)。所以我的 cron 作业实际上并没有找到我的脚本(因此有重复的项目)。

更重要的是,snap 是垃圾,并产生了许多我必须解决的问题(通过 apt 安装了 docker.io 版本),我的脚本现在可以正确执行。

相关内容