Getswap - Welcher Prozess liegt im Swap Space

Aus Laub-Home Wiki

Wer kennt das nicht, ein Linux System fängt langsam an den Swap zu füllen und diverse Dinge fühlen sich auf dem System einfach langsam an. Hier verschafft einem das getswap.sh einen Überblick darüber welche Prozesse gerade im swap liegen und welche nicht.

You can Download the script from GitHub:

Einfach in /usr/local/sbin speichern und die Execute Rechte vergeben:

cd /usr/local/sbin
wget https://raw.githubusercontent.com/alaub81/scripts/master/getswap.sh
chmod +x getswap.sh

hier das Script selbst:

/usr/local/sbin/getswap.sh

#!/bin/bash
#########################################################################
#Name: getswap.sh
#Subscription: Get current swap usage for all running processes
#by A. Laub
#andreas[-at-]laub-home.de
#original Script from Erik Ljungstrom 27/05/2011
#
#License:
#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 3 of the License, or (at your option)
#any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#or FITNESS FOR A PARTICULAR PURPOSE.
#########################################################################
#Set the language
export LANG="en_US.UTF-8"
#Load the Pathes
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

#set the variables:

#do something:
function all {
        SUM=0
        OVERALL=0
        for DIR in $(find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"); do
                PID=$(echo $DIR | cut -d / -f 3)
                PROGNAME=$(ps -p $PID -o comm --no-headers)
                        for SWAP in $(grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'); do
                        let SUM=$SUM+$SWAP
                done
                echo "PID=$PID - Swap used: $SUM - ($PROGNAME )"
                let OVERALL=$OVERALL+$SUM
                SUM=0
        done
echo "Overall swap used: $OVERALL"
}

case "$1" in
        all)
                all
                ;;
        mostused)
                all | sort -n -k 5
                ;;
        swaponly)
                all | egrep -v "Swap used: 0" |sort -n -k 5
                ;;
        *)
                echo "Usage: $0 { all | mostused | swaponly }"
                exit 1
                ;;
esac

Benutzt werden kann das Skript wie folgt:

# Spuckt einfach alle Prozesse aus mit und ohne Swap Benutzung
getswap.sh all
# Spuckt alle Prozesse aus mit und ohne Swap Benutzung und sortiert nach Swap Verbrauch
getswap.sh mostused
# Blendet Prozesse aus ohne Swap Benutzung und sortiert nach Swap Verbrauch
getswap.sh swaponly

Quellen