Skip to content

Instantly share code, notes, and snippets.

@Mebus
Created March 14, 2015 01:16
Show Gist options
  • Save Mebus/c6f9b62cf4fae88d7f73 to your computer and use it in GitHub Desktop.
Save Mebus/c6f9b62cf4fae88d7f73 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Get current swap usage for running processes
# Original: Erik Ljungstrom 27/05/2011
# Modifications by ariel:
# - Sort by swap usage
# - Auto run as root if not root
# - ~2x speedup by using procfs directly instead of ps
# - include full command line in output
# - make output more concise/clearer
# - better variable names
#
# Need root to look at all processes details
case `whoami` in
'root') : ;;
*) exec sudo $0 ;;
esac
(
PROC_SWAP=0
TOTAL_SWAP=0
for DIR in `find /proc/ -maxdepth 1 -type d | grep "^/proc/[0-9]"` ; do
PID=`echo $DIR | cut -d / -f 3`
CMDLINE=`cat /proc/$PID/cmdline 2>/dev/null | tr '\000' ' '`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null | awk '{ print $2 }'`
do
let PROC_SWAP=$PROC_SWAP+$SWAP
done
if [ $PROC_SWAP == 0 ]; then
# Skip processes with no swap usage
continue
fi
echo "$PROC_SWAP [$PID] $CMDLINE"
let TOTAL_SWAP=$TOTAL_SWAP+$PROC_SWAP
PROC_SWAP=0
done
echo "$TOTAL_SWAP Total Swap Used"
) | sort -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment