Fedora 42 更新脚本
发布: (2025年12月27日 GMT+8 19:18)
6 分钟阅读
原文: Dev.to
看起来您只提供了来源链接,而没有贴出需要翻译的正文内容。请把要翻译的文本(包括标题、段落、列表等)粘贴在这里,我会按照要求保留链接、代码块和 Markdown 格式进行简体中文翻译。
概览
一个用于更新 Fedora 42 操作系统的完整 Bash 脚本。它执行以下检查和操作:
- 验证互联网连接(下载更新所必需)
- 确保脚本以 root 身份运行(许多命令需要 sudo 权限)
- 检查数据库(MariaDB、MongoDB)和 Node.js 进程的状态
- 检测新的内核更新并评估
/boot中的可用空间(DNF 在此安装内核) - 启动更新过程
- 扫描 rootkit
- 列出已安装的内核,并在数量超过三个时给出警告
- 查找上一次启动时的内核错误
- 报告 journal 日志大小,并可选择清除超过一天的条目
- 显示
/tmp大小(systemd‑tmpfiles‑clean服务会清理/tmp) - 显示 DNF 缓存大小,并可选择清除它
- 报告
/var/log的大小
所有步骤均严格保持原脚本内容;仅对格式和少量排版错误进行了清理,以提升可读性。
主脚本
#!/bin/bash
clear
# set -e # Uncomment to stop the script on errors (good practice)
echo "-------------------------------------"
echo "Updates and maintenance for Fedora"
echo "-------------------------------------"
# -------------------------------------------------
# 1. System status and greeting
# -------------------------------------------------
# Check whether there is an internet connection
A=$(nmcli device status | grep eno1 | awk '{print $3}')
if [[ "$A" == "verbunden" ]]; then
echo "✅ Internet connection available on eno1."
else
echo "❌ NO Internet connection available, check your router."
fi
# Print the current date
DATUM=$(date +%x)
EINSCHALTZEIT=$(uptime -s | awk '{print $2}')
echo "Hallo $USER, heute ist der $DATUM. Der Rechner wurde um $EINSCHALTZEIT eingeschaltet."
echo
# Check whether we are root
BENUTZER="$USER"
if [[ "$BENUTZER" == "root" ]]; then
echo "Ich bin Root. Starte Wartungsprozesse."
# -------------------------------------------------
# 🛑 NEW: Status of databases and Node.js
# -------------------------------------------------
echo "----------------------------------------------------"
echo "Review of ongoing database and development processes"
echo "----------------------------------------------------"
# MariaDB check
if systemctl is-active mariadb &> /dev/null; then
echo "✅ MariaDB: Database server is running."
else
echo "❌ MariaDB: Database server is inactive."
fi
# MongoDB check
if systemctl is-active mongod &> /dev/null; then
echo "✅ MongoDB: Database server is running."
else
echo "❌ MongoDB: Database server is inactive."
fi
# Node.js check
NODE_COUNT=$(pgrep node | wc -l)
if [[ "$NODE_COUNT" -gt 0 ]]; then
echo "⚠️ Node.js: $NODE_COUNT Node.js process(es) are currently running!"
# List the PIDs, if needed
pgrep node
else
echo "✅ Node.js: No active Node.js processes found."
fi
echo "----------------------------------------------------"
# -------------------------------------------------
# 2. Maintenance and updates
# -------------------------------------------------
echo "----------------------------------------------------"
echo "Preliminary check: kernel updates & boot memory"
echo "----------------------------------------------------"
# 1. Check whether there is a new kernel
KERNEL_UPDATE=$(dnf check-update kernel -q)
if [[ -n "$KERNEL_UPDATE" ]]; then
echo "🆕 A new kernel update has been found!"
# 2. Check the size of /boot
BOOT_AVAIL=$(df -m /boot --output=avail | tail -1 | tr -d ' ')
# Safety margin: a new kernel requires approx. 150 MB (including reserve for initramfs)
SCHWELLE=200
echo "Verfügbarer Platz in /boot: ${BOOT_AVAIL} MB"
if (( BOOT_AVAIL /dev/null || true
echo "----------------------------------------------"
echo "Kontrolle der installierten Kernel"
# Count the installed kernel packages
KERNEL_ANZAHL=$(rpm -q kernel | wc -l)
echo "Aktuell sind $KERNEL_ANZAHL Kernel installiert:"
rpm -q kernel
# Small warning if the limit (3) is exceeded
if (( KERNEL_ANZAHL > 3 )); then
echo "⚠️ Hinweis: Mehr als 3 Kernel installiert. Prüfen Sie die dnf.conf!"
fi
echo "----------------------------------------------"
echo "----------------------------------------------"
# Kernel errors
echo "Prüfung auf Kernel‑Fehler im letzten Boot‑Vorgang:"
journalctl -kb -1 | grep -iE 'kernel panic|BUG:|oops|segfault|NMI watchdog'
echo ----------------------------------------------"
# Journal size check and vacuum
echo "Prüfung Journal‑Loggröße:"
journalctl --disk-usage
echo "Journal‑Logfiles löschen (älter als 1 Tag)"
journalctl --vacuum-time=1d
echo "----------------------------------------------"
# /tmp size check (syst
emd‑tmpfiles‑clean deletes the content of /tmp)
echo "Prüfung der /tmp‑Größe:"
du -sh /tmp
echo ----------------------------------------------"
# DNF cache size
echo "Prüfung DNF‑Cache‑Größe:"
dnf clean metadata && dnf clean packages
du -sh /var/cache/dnf
echo ----------------------------------------------"
# /var/log size
echo "Prüfung /var/log‑Größe:"
du -sh /var/log
echo ----------------------------------------------"
else
echo "❌ Dieses Skript muss als root ausgeführt werden."
exit 1
fi
所有步骤和检查都严格保留原始脚本的内容;仅对格式和少量排版问题进行了清理,以提升可读性。
额外清理步骤
# delete the dnf cache
echo "DNF Cache löschen"
dnf clean packages
dnf clean metadata
dnf clean dbcache
echo "----------------------------------------------"
# checking the /tmp folder size (Kann wegen systemd-tmpfiles-clean entfernt werden, aber der Check schadet nicht)
TMP_SIZE_KB=$(du -sk /tmp/ | awk '{print $1}')
THRESHOLD_KB=400 # 400 KB
echo "Prüfung /tmp Ordnergröße"
if (( TMP_SIZE_KB > THRESHOLD_KB )); then
echo "⚠️ Warnung: /tmp ist größer als $THRESHOLD_KB KB. Aktuelle Größe: $TMP_SIZE_KB KB"
else
echo "✅ Die Größe von /tmp ist unter $THRESHOLD_KB KB. Aktuelle Größe: $TMP_SIZE_KB KB"
fi
echo "---------------------------------------------"
# Check DNF cache size
ORDNER_DNF="/var/cache/dnf"
SIZE_MB_DNF=$(du -sm "$ORDNER_DNF" | cut -f1)
LIMIT_MB_DNF=1024 # 1 GB
echo "Prüfung DNF Cache Größe:"
echo "Aktuelle DNF Cache Größe: $(du -sh "$ORDNER_DNF" | cut -f1)"
if [ "$SIZE_MB_DNF" -gt "$LIMIT_MB_DNF" ]; then
echo "Achtung: Der Ordner '$ORDNER_DNF' ist größer als 1 GB."
else
echo "✅️ Der Ordner '$ORDNER_DNF' ist unter 1 GB."
fi
echo
echo "---------------------------------------------"
# Check the size of /var/log
ORDNER_LOG="/var/log"
SIZE_MB_LOG=$(du -sm "$ORDNER_LOG" | cut -f1)
LIMIT_MB_LOG=1024 # 1 GB
echo "Prüfung /var/log Ordnergröße:"
echo "Aktuelle /var/log Größe: $(du -sh "$ORDNER_LOG" | cut -f1)"
if [ "$SIZE_MB_LOG" -gt "$LIMIT_MB_LOG" ]; then
echo "Achtung: Der Ordner '$ORDNER_LOG' ist größer als 1 GB."
else
echo "✅ Der Ordner '$ORDNER_LOG' ist unter 1 GB."
fi
echo
echo "----------------------------------------------"
echo
echo "Finish, Ende, an die Arbeit 💼️"
else
echo "❌ Error: I currently do not have root privileges. The update script requires root privileges."
fi