From edda92abc4ae1eaca39c6c389e03aa7679c5d839 Mon Sep 17 00:00:00 2001 From: mini2024 Date: Sat, 22 Mar 2025 17:09:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20upgrade.sh=20=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=EF=BC=8C=E7=AE=80=E5=8C=96=E8=87=AA=E5=8A=A8=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=85=8D=E7=BD=AE=E5=B9=B6=E6=B7=BB=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E6=A3=80=E6=9F=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debian/upgrade.sh | 95 ++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 54 deletions(-) diff --git a/debian/upgrade.sh b/debian/upgrade.sh index 1a1a770..4de6669 100644 --- a/debian/upgrade.sh +++ b/debian/upgrade.sh @@ -7,10 +7,13 @@ # 设置非交互式环境变量 export DEBIAN_FRONTEND=noninteractive -export NEEDRESTART_MODE=a + +apt -qq update # 安装自动更新 apt install -y unattended-upgrades +# 启用自动更新 +dpkg-reconfigure -plow unattended-upgrades # 配置自动更新并在凌晨2点自动重启 cat < +# 方法2: 检查正在运行的内核与已安装的最新内核是否不同 +CURRENT_KERNEL=$(uname -r) +LATEST_KERNEL=$(dpkg -l 'linux-image-*' | grep ^ii | grep -v "${CURRENT_KERNEL}" | sort -V | tail -n 1 | awk '{print $2}' | sed 's/linux-image-//') -# Copyright Holder: -# 2013 - 2022 (C) Thomas Liske [http://fiasko-nw.net/~thomas/] +if [ ! -z "$LATEST_KERNEL" ] && [ "$LATEST_KERNEL" != "$CURRENT_KERNEL" ]; then + NEEDS_REBOOT=1 + REASONS="${REASONS}\n已安装新内核: ${LATEST_KERNEL},当前运行内核: ${CURRENT_KERNEL}" +fi -# Upstream: -# https://github.com/liske/needrestart +# 方法3: 使用 needrestart 工具(如果已安装) +if command -v needrestart &> /dev/null; then + if needrestart -b | grep -q "NEEDRESTART-KSTA: 1"; then + NEEDS_REBOOT=1 + REASONS="${REASONS}\nneedrestart 工具表明需要内核重启" + fi +fi -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. - -# Usage: - -# needrestart [-vn] [-c ] [-r ] [-f ] [-u ] [-bkl] - -# -v be more verbose -# -q be quiet -# -m set detail level -# e (e)asy mode -# a (a)dvanced mode -# -n set default answer to 'no' -# -c config filename -# -r set restart mode -# l (l)ist only -# i (i)nteractive restart -# a (a)utomatically restart -# -b enable batch mode -# -p enable nagios plugin mode -# -f override debconf frontend (DEBIAN_FRONTEND, debconf(7)) -# -t tolerate interpreter process start times within this value -# -u use preferred UI package (-u ? shows available packages) - -# By using the following options only the specified checks are performed: -# -k check for obsolete kernel -# -l check for obsolete libraries -# -w check for obsolete CPU microcode - -# --help show this help -# --version show version information +# 输出结果 +if [ $NEEDS_REBOOT -eq 1 ]; then + echo -e "系统需要重启\n原因:\n${REASONS}" + exit 1 +else + echo "系统不需要重启" + exit 0 +fi \ No newline at end of file