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