我們在直接執行腳本是可以的,但是使用cron沒有效果, 查看日志(/var/log/cron)會有ORPHAN (no passwd entry)報錯
原因: 環境變量
解決方案
1、所有命令需要寫成絕對路徑形式,如: /usr/local/bin/docker。
2、在 shell 腳本開頭使用以下代碼:
#!/bin/sh . /etc/profile . ~/.bash_profile
3、在 /etc/crontab 中添加環境變量,在可執行命令之前添加命令 . /etc/profile;/bin/sh,使得環境變量生效,例如:
20 03 * * * . /etc/profile;/bin/sh /var/www/runoob/test.sh
參考文獻: https://www.runoob.com/linux/linux-comm-crontab.html