Skip to content

Instantly share code, notes, and snippets.

@kolayne
Last active March 31, 2024 20:06
Show Gist options
  • Save kolayne/4b470cf7ce4e4128e2fdac1d73ce709f to your computer and use it in GitHub Desktop.
Save kolayne/4b470cf7ce4e4128e2fdac1d73ce709f to your computer and use it in GitHub Desktop.
Analyze swap usage per process
#!/bin/bash -i
# Get current swap usage for all running processes
# Originally written by Erik Ljungstrom 27/05/2011
# Modified by Nikolay Nechaev aka @kolayne
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | grep -E "^/proc/[0-9]"` ; do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o cmd --no-headers` # | cut -d ' ' -f 1`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'`
do
let SUM=$SUM+$SWAP
done
if [[ $SUM -eq 0 ]]; then
continue
fi
echo -e "PID=$PID\t- Swap used: $SUM\t- $PROGNAME" | expand -t 8 | cut -c-$(($COLUMNS - 1))
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo "Overall swap used: $OVERALL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment