在linux中使用Bash zombie进程脚本

时间:2016-06-23 04:26:27

标签: linux bash kill-process zombie-process

我想写一个脚本来检测和杀死僵尸进程。实施将是杀死子进程和父进程。该脚本应在特定用户下每小时运行一次,当检测到僵尸时,会生成一条日志消息,并将其发送到日志目录,以便在kill成功且未成功时。

我的问题: 如何在脚本中实现每小时条件? 如何确保在我的脚本中生成并实现我的日志消息? 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

0 个答案:

没有答案