Home > bash scripting, Solaris > Kill Zombie Process on Solaris 10 (or another *nix family)

Kill Zombie Process on Solaris 10 (or another *nix family)

    Requirement :

  1. Paham konsep Operating System secara umum.
  2. Paham konsep “process” pada unix family, utamanya Solaris.
  3. Terbiasa menggunakan OS Solaris10.
  4. Mau membaca & mencoba tulisan ini 😀

Attention :

Tulisan ini adalah pengalaman pribadi saya, kalaupun bisa menjadi tips bagi pembaca sekalian ya alhamdulillah, tapi klo systemnya menjadi error saya tidak bersedia untuk dikejar-kejar.

Definisi zombie process

dari Wikipedia :
“On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the parent process to read its child’s exit status. The term zombie process derives from the common definition of zombie — an undead person. In the term’s metaphor, the child process has “died” but has not yet been “reaped”. Also, unlike normal processes, the kill command has no effect on a zombie process.”

Oleh karena itu judul yang saya tulis diatas sebenarnya tidak “relevan” hanya saja saya memang memaksakan untuk menuliskan sebagai KILL zombie process, bukan REAP zombie process, ataupun EAT zombie process. Tetapi saya lebih memilih judul diatas agar terdengar lebih BOMBASTIS 🙂

#!/bin/ksh

#
# zombie_manager.sh - script to manage zombie process
# please check LOG variable defined below
# for script's log file
#
# $NewRev$
# 120815 LQman: create zombie_manager script
# $NewRev$
#

ZOMBIE_LIST=`ps -ef | grep defunct | grep -v grep`
LOG="/var/log/`basename $0`.log"
DATETIME=`date +%F_%H:%M:%S`

# script must be run with ROOT privileges
if [ "`/usr/local/bin/whoami`" != "root" ]; then
echo "You must be root to execute `basename $0`" 2>&1
exit 1
fi

output()
{
echo "$1"
echo "$1" >> $LOG
}

# switch between arguments
case $1 in
show)
# show banner
echo "*************** Script started at $DATETIME ***************" >> $LOG

# count zombie process
ZOMBIE_SUM=`echo "$ZOMBIE_LIST" | wc | awk '{print $1}'`

# display summary of zombie process
if [ "$ZOMBIE_LIST" ]; then
output "Found $ZOMBIE_SUM Zombie Process"
output "$ZOMBIE_LIST"
else
output "no Zombie Process Found"
exit 2
fi
;;
clear)
# show zombie PID list first before kill it
$0 show
# get zombie PID
ZOMBIE_PID=`echo "$ZOMBIE_LIST" | awk '{print $2}'`

# if no zombie PID found then exit
if [ ! "$ZOMBIE_PID" ]; then
exit 2
fi

# reap all zombie PID one-by-one
for zombie in `echo "$ZOMBIE_PID"`
do
output "Reaping PID $zombie : \c"

# reap zombie and redirect stdout & stderr to $LOG
preap $zombie >> $LOG 2>&1
if [ "$?" == "0" ]; then
output "Success"
else
output "Failed, exit code $?"
fi
done
;;
*)
# Help user to use script
output "$0: unknown directive '$1'"
output "Usage: $0 [show|clear]"
exit 3
;;
esac

exit 0

Add to Crontab

Run scripts automagically every midnight with crontab.

lqman@documentation:~$ crontab -e
0 0 * * * /home/lqman/scripts/zombie_manager.sh clear

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: