Skip to content

Instantly share code, notes, and snippets.

@aramisf
Forked from eddy85br/swapgrep.sh
Last active August 29, 2015 14:19
Show Gist options
  • Save aramisf/c4fc8be6691826dfc6cf to your computer and use it in GitHub Desktop.
Save aramisf/c4fc8be6691826dfc6cf to your computer and use it in GitHub Desktop.
#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
#
## Adapted by Jhonatan Piffer Siqueira and Eduardo Lemons Francisco (eddy85br).
OVERALL=0
PROGLIST=$(ps axw -o pid,args --no-headers)
while read PID ARGS; do
SUM=0
if [ -f "/proc/$PID/smaps" ]; then
for SWAP in $(fgrep 'Swap' /proc/$PID/smaps 2>/dev/null | awk '{ print $2 }') ; do
let SUM=$SUM+$SWAP
done
fi
if [[ $SUM > 0 ]]; then
printf "PID: %-6s | Swap used: %-6s KB => %s\n" $PID $SUM "$ARGS"
else
printf "Not using Swap, PID: %-6s => %s\n" $PID "$ARGS" 1>/dev/stderr
fi
let OVERALL=$OVERALL+$SUM
done <<<"$PROGLIST"
echo "Overall swap used: $OVERALL"
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment