如何实现Ubuntu自动更新并重启?

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
没有评论

发表回复

Debian-Like
如何安装Ubuntu Linphone?

1 基础知识 1.1 软件介绍 Linphone 是一款开源的 VoIP(Voice over In …

Debian-Like
如何安装配置Ubuntu iptables-persistent

1 前言 一个问题,一篇文章,一出故事。 笔者今天需要在Ubuntu使用iptables,于是整理当 …

Debian-Like
如何安装Ubuntu steam?

1 基础知识 Steam是Valve公司聘请BitTorrent(BT下载)发明者布拉姆·科恩(Br …