更新 upgrade.sh 脚本,简化自动更新配置并添加重启检查功能

This commit is contained in:
mini2024
2025-03-22 17:09:05 +08:00
parent 56b9b917cd
commit edda92abc4

95
debian/upgrade.sh vendored
View File

@ -7,10 +7,13 @@
# 设置非交互式环境变量 # 设置非交互式环境变量
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
export NEEDRESTART_MODE=a
apt -qq update
# 安装自动更新 # 安装自动更新
apt install -y unattended-upgrades apt install -y unattended-upgrades
# 启用自动更新
dpkg-reconfigure -plow unattended-upgrades
# 配置自动更新并在凌晨2点自动重启 # 配置自动更新并在凌晨2点自动重启
cat <<EOF | tee /etc/apt/apt.conf.d/50unattended-upgrades cat <<EOF | tee /etc/apt/apt.conf.d/50unattended-upgrades
@ -19,16 +22,6 @@ Unattended-Upgrade::Automatic-Reboot-WithUsers "true";
Unattended-Upgrade::Automatic-Reboot-Time "02:00"; Unattended-Upgrade::Automatic-Reboot-Time "02:00";
EOF EOF
# 配置 needrestart 为非交互式
cat <<EOF | tee /etc/needrestart/conf.d/50noninteractive.conf
\$nrconf{restart} = 'a';
\$nrconf{kernelhints} = -1;
EOF
# 启用自动更新
dpkg-reconfigure -plow unattended-upgrades
apt -qq update
apt -qq upgrade -y apt -qq upgrade -y
apt -qq full-upgrade -y apt -qq full-upgrade -y
apt -qq autoremove apt -qq autoremove
@ -36,52 +29,46 @@ apt -qq autoclean
apt clean apt clean
systemctl --failed systemctl --failed
apt -qq install needrestart -y apt -qq install needrestart -y
needrestart -r a
################################################################################ ############################################
# needrestart --help # 设置变量标记是否需要重启
NEEDS_REBOOT=0
REASONS=""
# needrestart 3.6 - Restart daemons after library updates. # 方法1: 检查 reboot-required 文件
if [ -f /var/run/reboot-required ]; then
NEEDS_REBOOT=1
# 如果有详细信息文件,显示需要重启的包
if [ -f /var/run/reboot-required.pkgs ]; then
REASONS="需要重启的包:\n$(cat /var/run/reboot-required.pkgs)"
else
REASONS="系统标记为需要重启"
fi
fi
# Authors: # 方法2: 检查正在运行的内核与已安装的最新内核是否不同
# Thomas Liske <thomas@fiasko-nw.net> 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: if [ ! -z "$LATEST_KERNEL" ] && [ "$LATEST_KERNEL" != "$CURRENT_KERNEL" ]; then
# 2013 - 2022 (C) Thomas Liske [http://fiasko-nw.net/~thomas/] NEEDS_REBOOT=1
REASONS="${REASONS}\n已安装新内核: ${LATEST_KERNEL},当前运行内核: ${CURRENT_KERNEL}"
fi
# Upstream: # 方法3: 使用 needrestart 工具(如果已安装)
# https://github.com/liske/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 if [ $NEEDS_REBOOT -eq 1 ]; then
# the Free Software Foundation; either version 2 of the License, or echo -e "系统需要重启\n原因:\n${REASONS}"
# (at your option) any later version. exit 1
else
# Usage: echo "系统不需要重启"
exit 0
# needrestart [-vn] [-c <cfg>] [-r <mode>] [-f <fe>] [-u <ui>] [-bkl] fi
# -v be more verbose
# -q be quiet
# -m <mode> set detail level
# e (e)asy mode
# a (a)dvanced mode
# -n set default answer to 'no'
# -c <cfg> config filename
# -r <mode> 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 <fe> override debconf frontend (DEBIAN_FRONTEND, debconf(7))
# -t <seconds> tolerate interpreter process start times within this value
# -u <ui> 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