Files
Linux/debian/upgrade.sh

69 lines
1.6 KiB
Bash

#!/usr/bin/env bash
#
# ...
#
# 使用方法:
# URL="https://git.1-h.cc/Scripts/Linux/raw/branch/main/debian/upgrade.sh"; curl -fsSL "$URL" | bash
# 设置非交互式环境变量
export DEBIAN_FRONTEND=noninteractive
apt -qq update
# 安装自动更新
apt install -y unattended-upgrades
# 启用自动更新
dpkg-reconfigure -plow unattended-upgrades
# 配置自动更新并在凌晨2点自动重启
cat <<EOF | tee /etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-WithUsers "true";
Unattended-Upgrade::Automatic-Reboot-Time "02:00";
EOF
apt -qq upgrade -y
apt -qq full-upgrade -y
apt -qq autoremove
apt -qq autoclean
apt clean
systemctl --failed
apt -qq install needrestart -y
############################################
# 初始化变量
NEEDS_REBOOT=0
REASONS=""
# 函数: 添加重启原因
add_reason() {
if [ -z "$REASONS" ]; then
REASONS="$1"
else
REASONS="$REASONS\n$1"
fi
NEEDS_REBOOT=1
}
echo "检查系统重启状态..."
# 方法1: 检查 reboot-required 文件 (最可靠的方法)
if [ -f /var/run/reboot-required ]; then
add_reason "发现 /var/run/reboot-required 文件"
# 如果存在详细信息文件,显示需要重启的包
if [ -f /var/run/reboot-required.pkgs ]; then
add_reason "需要重启的包:\n$(cat /var/run/reboot-required.pkgs)"
fi
fi
# 输出结果
if [ $NEEDS_REBOOT -eq 1 ]; then
echo -e "\033[1;31m系统需要重启\033[0m"
echo -e "\033[1;33m原因:\033[0m"
echo -e "$REASONS"
exit 1
else
echo -e "\033[1;32m系统不需要重启\033[0m"
exit 0
fi