Find out which processes are Swapping

From Zarafa wiki

Revision as of 10:59, 22 September 2011 by Msartor (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Ever wanted to know which processes are swapping and how much swap space they are using. Starting from kernel 2.6.16, we can find out using smaps which can be found in the proc filesystem. The script below will make it easy for you and lists all processes and how much swap space they are using.


The script:

#!/bin/bash
#

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}K - ($PROGNAME )"
  let OVERALL=$OVERALL+$SUM
  SUM=0
done

echo "Overall swap used: $OVERALL"
Personal tools