I want to write a script to detect and kill zombie processes. The implementation would be to kill the child and parent process. The script should run hourly under a specific user and when a zombie is detected a log message is generated and sent to a log directory for when the kill is successful and not successful.
My Question: How can I implement the hourly condition in my script? How can make sure my log messages are generated and implemented in my script? log dir “var/log/zombie.log”
#script will kill all the child process id for a given pid
#Store the current Process ID
CURPID=$$
`enter code here`#This is process id, parameter passed by user
ppid=$1
if [ -z $ppid ] ; then
echo No PID given.
exit;
fi
arraycounter=1
while true
do
FORLOOP=FALSE
#Get all the child process id
for i in `ps -ef| awk '$3 == '$ppid' { print $2 }'`
do
if [ $i -ne $CURPID ] ; then
procid[$arraycounter]=$i
arraycounter=`expr $arraycounter + 1`
ppid=$i
FORLOOP=TRUE {
printf("Detected process : %d from process <%d> : User <%d>\n",i, getpid(), getuser() "Successfully Killed");
else
printf("Detected process : %d from process <%d> : User <%d>\n",i, getpid(), getuser() "could not kill");
}
fi
done
if [ "$FORLOOP" = "FALSE" ] ; then
arraycounter=`expr $arraycounter - 1`
## We want to kill child process id first and then parent id's
while [ $arraycounter -ne 0 ]
do
kill -9 "${procid[$arraycounter]}" >/dev/null
arraycounter=`expr $arraycounter - 1`
done
exit
fi
done
## Kill Parent ID
kill -9 $CURPID