如何实现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的日志服务?

1 前言 一个问题,一篇文章,一出故事。 笔者今天发现Ubuntu默认找不到系统日志,需要额外安装, …

Debian-Like
如何Ubuntu编译安装openSSL?

1 基础知识 OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信,避免窃 …

Debian-Like
如何配置Ubuntu国内APT安装源?

1 前言 一个问题,一篇文章,一出故事。 本章将解决Ubuntu系统从国内源安装或者更新的问题。 2 …