0

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
  • 1
    "How can I implement the hourly condition in my script?". Does it have to be in your script? [cron](http://man7.org/linux/man-pages/man8/cron.8.html) is the common way for scheduling time based runs. No need to reinvent the wheel if that is adequate. – kaylum Jun 23 '16 at 04:40
  • @kaylum any example of how i can schedule the time based run? – user3613649 Jun 23 '16 at 13:01
  • Yes, just read the man page already provided or do a internet search for "cron examples" and there will be more than you can read. – kaylum Jun 23 '16 at 20:25

0 Answers0