The core server ran a bunch of scripts, procedures, it is inevitable that sometimes zombie process, dead or alive in the occupied resources there, initially just wrote a keyword to kill the process of the Linux shell script, and later found that many times when the process died there is actually the internal call to the child process when there is a problem , the process of killing the father does not solve the fundamental problem. For example, when rsync is connected via SSH, rsync itself is fine, but maybe SSH dies. So the script is written back, recursively looking for the subprocess.
Copy Code code as follows:
#!/bin/sh
# recursion finds the bottommost subprocess that causes the process zombie and kills it.
parentprocessid=$1;
if ["x${parentprocessid}" = "X"]; Then
Echo ' Please Supply the ' top Parent Process ID to be killed! '
echo "Usage:sh $ PID [-v]"
echo "PID the Parent Process ID as root"
echo "-V-is"-argument supplied,no real kill operation'll be performed,only the process tree being show. "
Exit 1
Fi
Let Isrealkilldo=1;
If ["x$2" = "x-v"]; Then
Let isrealkilldo=0;
Fi
echo "Begin Kill the Leaf process of process ${parentprocessid}" >&2
Killpidlist= ""
function loopnextsubprocess () {
Local nparentprocessid=$1
Local tmppidlist= ""
Tmppidlist= ' ps-a--format=%p%pisparent '--width 2048-w--sort pid|grep ' ${nparentprocessid}isparent ' |grep-v Grep-v "$$" | awk ' {printf $} '
PS--format= '%p%p%a '--width 2048-w-P ${nparentprocessid}|grep-v grep|grep-v ' $$ ' >&2
if ["x${tmppidlist}" = "X"]; Then
echo "****got One Leaf = [${nparentprocessid}]****" >&2
Killpidlist= "${killpidlist}\n${nparentprocessid}"
Return
Fi
For thenextpid in ${tmppidlist}; Todo
Loopnextsubprocess ${thenextpid}
Done
}
Loopnextsubprocess ${parentprocessid}
If [${isrealkilldo}-eq 1-a "x${killpidlist}"!= "X"]; Then
For curpid in ' echo-e ${killpidlist} '; Todo
if ["X${curpid}"!= "X"]; Then
echo "Kill-9 ${curpid}"
Kill-9 ${curpid}
Fi
Done
Else
Echo-e ${killpidlist}
Fi