参: @僵尸进程问题

1:如何产生:
僵尸进程,子进程先于父进程结束,而父进程未用wait或waitpid来监听子进程的状态,使子进程的资源无法释放。
孤儿进程:父进程先于子进程结束。

2:作用
僵尸进程:几乎没有什么作用,像 @浪际天涯 提到的一样,不会被调度。
孤儿进程:最经典的一个作用应该是避免产生僵尸进程,通过fork两次的方式,刻意杀掉其父进程,使之成为守护进程。

3:如何杀死僵尸进程
copy @浪际天涯

4:杀死僵尸进程的shell脚本
再提供一个网络版本: Automatic “zombie” processes killing (shell script)

   
  #!/bin/bash
  
# Zombie processes 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

卡特蕾亚大死机 answered 12 years, 10 months ago

Your Answer