How to findout which processes are swapping in linux
The below script will give the exact swap usage result on the server.
=–=-=-===-=-=-=-
#!/bin/bash
# Get current swap usage for all running processes
# Pipe the output to “sort -nk3” to get sorted output
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d -regex “^/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
if (( $SUM > 0 )); then
echo “PID=$PID swapped $SUM KB ($PROGNAME)”
fi
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo “Overall swap used: $OVERALL KB”
=–=-=-===-=-=-=-
The script will show which process is taking swap usage.
[~]# sh swap.sh
PID=1 swapped 80 KB (init)
PID=640 swapped 324 KB (udevd)
PID=1266 swapped 684 KB (crond)
PID=1268 swapped 492 KB (sshd)
PID=1272 swapped 16136 KB (php)
PID=2194 swapped 56 KB (iscsid)
PID=2986 swapped 44 KB (syslogd)
PID=2989 swapped 76 KB (klogd)
PID=3220 swapped 36 KB (hald-addon-stor)
PID=3268 swapped 268 KB (automount)
PID=3290 swapped 8364 KB (named)
PID=3344 swapped 152 KB (xinetd)
PID=3820 swapped 448 KB (pure-ftpd)
PID=3822 swapped 316 KB (pure-authd)
PID=3929 swapped 200 KB (crond)
PID=3942 swapped 100 KB (atd)
PID=4603 swapped 424 KB (imap-login)
Overall swap used: 194024 KB