From afadfffda223b2978d78036fc2db7ad65792cb72 Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Mon, 23 Feb 2026 19:55:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0SWAP=E5=8D=A0=E7=94=A8?= =?UTF-8?q?=E7=8E=87=E6=9F=A5=E8=AF=A2=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- archrived-shells/showswap.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 archrived-shells/showswap.sh diff --git a/archrived-shells/showswap.sh b/archrived-shells/showswap.sh new file mode 100644 index 0000000..d98176c --- /dev/null +++ b/archrived-shells/showswap.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# 获取总 Swap(单位 kB) +total_swap_kb=$(grep SwapTotal /proc/meminfo | awk '{print $2}') + +if [ "$total_swap_kb" -eq 0 ]; then + echo "系统未启用 Swap" + exit 1 +fi + +printf "%-8s %-12s %-25s %-12s %-8s\n" "PID" "USER" "PROCESS" "SWAP(MB)" "USAGE%" + +for pid in $(ls /proc | grep -E '^[0-9]+$'); do + if [ -r /proc/$pid/status ]; then + swap_kb=$(grep VmSwap /proc/$pid/status 2>/dev/null | awk '{print $2}') + if [ ! -z "$swap_kb" ] && [ "$swap_kb" -gt 0 ]; then + user=$(ps -o user= -p $pid 2>/dev/null) + comm=$(ps -o comm= -p $pid 2>/dev/null) + + swap_mb=$(awk "BEGIN {printf \"%.2f\", $swap_kb/1024}") + percent=$(awk "BEGIN {printf \"%.2f\", ($swap_kb/$total_swap_kb)*100}") + + printf "%-8s %-12s %-25s %-12s %-8s\n" \ + "$pid" "$user" "$comm" "$swap_mb" "$percent" + fi + fi +done | sort -k4 -nr \ No newline at end of file