Catch Zombie Process

Hi All,
Anyone have any shell script to capture the zombie process, as according to the support they need the real time zombie PID, they only provide the
kdb

(0) >  p* |grep -i defunct 

(0) > p * | grep <hex pid>

But this is doesn't seem easy to catch the zombie as it is not always appear in the process.

wish someone can shed some light on this. Thanks

Yeah...it's not that simple.

#!/bin/bash
# zkill - zombie process killing script
# Must be run under root.
case "$1" in
--admin)
        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"`
 
        if ((${#stat} > 0));then
    	    echo zombie processes found:
	    echo .
	    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"
	    echo -n "Kill zombies? [y/n]: "
	    read keyb
	    if [ $keyb == 'y' ];then
		echo killing zombies..
		ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print$4" status:"$10}' | grep "status:Z" | awk '{print $1}' | xargs -n 1 kill -9
	    fi
	else
	    echo no zombies found!
	fi
;;
--cron)
	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"`
        if ((${#stat} > 0));then
        ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print$4" status:"$10}' | grep "status:Z" | awk '{print $1}' | xargs -n 1 kill -9
	echo `date`": killed some zombie proceses!" >> /var/log/zombies.log
	fi
;;
*)	echo 'usage: zombies {--cron|--admin}'
;;
esac
exit 0

Best thing to do is run this under cron so you'll be copying this script (assuming you have root privileges) to /bin and then make a cronjob to run it as often as you think it necessary to run.

Hope that helps.