Monday, 17 August 2015

Shell script to kill Zombie process

Zombie process is a process state when the child dies before the parent process.

To see zombie process:
Run “ps aux” and look for a Z in the STAT column.
It also indicates as  “<defunct>” after the process information when we run “ps -aef”.

here is the link to get complete details of Zombie process.
https://zombieprocess.wordpress.com/what-is-a-zombie-process/

Shell script to stop all Zombie process:

We can find and remove all zombie process from our Linux system.
http://www.linuxscrew.com/wp-content/uploads/2007/09/zombies.sh.txt

Shell script to stop Specific Zombie process: 

We can also find and remove specific zombie process, in my case I am using thinking sphinx and below is the shell script to find and stop sphinx zombie process:


#!/bin/bash

release_path() {
  cd <application current path>
}

stat=`ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Z.*searchd"`

if ((${#stat} > 0));then
  ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Z.*searchd" | awk '{print $5}' | xargs -n 1 kill -9
  echo `date`" ::: killed thinking sphinx zombie process!" >> /var/log/zombies.log
  
  sphinx_stat=`ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Sl.*searchd"`

  if ((${#sphinx_stat} > 0));then
    ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Sl.*searchd" | awk '{print $2}' | xargs -n 1 kill -9
  fi
  
  release_path
  RAILS_ENV=production <run your rails specific command>
fi
 

Above script will find the sphinx zombie process and then kill that process, after that we are finding the sphinx process that is still running and kill that process as well.
You can also run your rails specific command in that file.

Run your shell script:

Save this file with extension “.sh” and give it permission “chmod +x <file name>.sh”, and put this file to specific folder.

You can directly run this script by the command on the file directory “./<file name>.sh

Or you can add this shell script to your crontab file.


Thanks!

No comments:

Post a Comment