Shell script implements killing subprocess, zombie process _linux shell

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.