Determine which process is using swap

Reason 2 why you should give me some BitCoins... I'm poor?.... If this helped you in any way and you have some spare BitCoins, you may donate them to me - 16tb2Rgn4uDptrEuR94BkhQAZNgfoMj3ug

My server recently ran out of swap space, causing slow calculations, slow response times etc

cd /opt/temp

vi a script called swap.sh, paste the following and save with ESC:wq!
#!/bin/bash
# Get current swap usage for all running processes
# This script will tell you the PID and the command line
# that stared the PID with how much of swap space is being used
# King Rat 08/02/2014
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 "****************************"
echo "PID=$PID swapped $SUM in KB ($PROGNAME)"
echo "cmd line that activate the PID is below "
xargs -0 echo < /proc/$PID/cmdline
echo "*****************************************"
fi
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo " "
echo "Overall swap used: $OVERALL in KB"
let OVERALL=$OVERALL/1024
echo "Overall swap used: $OVERALL in MB"
echo " "
echo "******** swapon -s *********"
swapon -s
echo " "
echo "********  free -m  *********" 
free -m

Make the script executable with chmod +x swap.sh Run the script with ./swap.sh, see output below
someserver:/opt/temp # ./swap.sh
****************************
PID=1 swapped 104 in KB (init)
cmd line that activate the PID is below
init [3] 
*****************************************
****************************
PID=708 swapped 16 in KB (oracle)
cmd line that activate the PID is below
ora_w006_DTE
*****************************************
****************************
PID=1504 swapped 28 in KB (cron)
cmd line that activate the PID is below
/usr/sbin/cron
*****************************************

Bla, Bla, Bla

****************************
PID=63545 swapped 12 in KB (oracle)
cmd line that activate the PID is below
oracle (LOCAL=NO)
*****************************************
****************************
PID=63568 swapped 12 in KB (oracle)
cmd line that activate the PID is below
ora_w007
*****************************************
****************************
PID=64038 swapped 16 in KB (oracle)
cmd line that activate the PID is below
ora_w00d
*****************************************
  
Overall swap used: 310456 in KB
Overall swap used: 303 in MB
  
******** swapon -s *********
Filename                Type        Size    Used    Priority
/dev/mapper/system-swap                 partition    26214392    4371104    -1
  
********  free -m  *********
             total       used       free     shared    buffers     cached
Mem:         96714      95321       1393          0         50      71053
-/+ buffers/cache:      24217      72497
Swap:        25599       4268      21331
someserver:/opt/temp #

No comments:

Post a Comment

Note: only a member of this blog may post a comment.