Skip to content

Instantly share code, notes, and snippets.

@eddy85br
Created April 17, 2015 15:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eddy85br/afed2d03018c570b308e to your computer and use it in GitHub Desktop.
Save eddy85br/afed2d03018c570b308e to your computer and use it in GitHub Desktop.
View Swap Usage for running processes
#!/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