如何实现Ubuntu自动更新并重启?
- By : Will
- Category : Debian-Like
Debian-Like
1 前言
一个问题,一篇文章,一出故事。
笔者最近需要实现自动更新并重启的脚本,于是整理本章节。
2 最佳实践
2.1 创建脚本
vim ~/scripts/update_and_reboot.sh
加入如下配置,
#!/bin/bash
# 更新系统
echo "开始更新系统..."
apt update
UPGRADE_OUTPUT=$(apt full-upgrade -y)
apt autoremove -y
# 检查输出中是否包含重启提示
if echo "$UPGRADE_OUTPUT" | grep -q "You should reboot your computer to complete the upgrade."; then
echo "检测到需要重启,系统将在 5 秒后重启..."
sleep 5
reboot
else
echo "没有检测到需要重启,跳过重启."
fi
2.2 测试脚本执行
bash ~/scripts/update_and_reboot.sh
2.3 设置计划任务
crontab -e
加入如下设置,
0 3 * * * bash ~/scripts/update_and_reboot.sh
没有评论