任务更新在重启时不起作用,给我这个错误消息:"Temporary failure resolving 'security.ubuntu.com' "

Tasked update doesn't work at reboot time, giving me this error message: "Temporary failure resolving 'security.ubuntu.com' "

我正在设置一个 ubuntu 服务器,我需要做的一件事是在重新启动时更新所有已安装的软件包。我为此使用了 crontab。如果我手动 运行 脚本它工作正常,但是当我重新启动我的服务器时它有时工作,有时不工作。

我怀疑我的网络还没有启动,在重新启动后立即启动,我猜它不会花费相同的时间来启动它并且 运行每次重新启动都需要这样的时间为什么脚本有时会起作用?

这是我更新脚本的主要部分:

apt-get update -y | tee -a /var/log/update_script.log
apt-get upgrade -y | tee -a /var/log/update_script.log

这是我的 crontab 任务:

@reboot /home/shendric/update.sh

所以每次我 boot/reboot 我的服务器我都希望这个脚本 运行 并且我需要将输出放入日志文件中。谁能帮帮我?

我测试了

sleep 10

在我的代码中查看网络是否需要一些时间来启动并且脚本每次都有效。 所以我只是在我的脚本中添加了一个检查互联网连接的条件。 如果网络尚未启动,它会等待 10 秒。否则它会照常更新。 这是我的代码:

if ping -q -c 1 -W 1 8.8.8.8 >/dev/null;
then
    update_function
else
    sleep 10
    update_function
fi

希望这对可能遇到相同问题的任何人有所帮助!