Zombie Process Slayer - Script to kill defunct processes

Here is a small script that you can use to show you which processes are zombies and which user is attached to that process and also it will ask if you want to terminate the process. (thanks to nixcraft [DOT] com)





#!/usr/bin/bash
#
# Zombie Slayer
#
# zombie_slayer [OPTIONS]
#
# -d
#   Just diplay the zombie processes and exit
#
# -h | help
#   Display help
#
# Default is to show processes and the ask what to kill
#

function Z_Display
{
    echo 
""
    
echo "PID - PPID - State - User - Proc"
    
UNIX95ps -eo pid,ppid,state,user,comm awk 'BEGIN { count=0 } $3 ~ /Z/ { count++; print $1,$2,$3,$4,$5 } END { print "\n" count " Zombie(s) to slay." }'
    
echo ""
}

function 
Z_Kill
{
    
read -"Enter PPID to kill or 'exit' : " SLAY_PPID
    
if [ "$SLAY_PPID"exit" ] || [ "$SLAY_PPID"" ]
    
then
        
exit
    
fi
    ps 
-p $SLAY_PPID grep -q $SLAY_PPID
    
if [ $? -eq 0 ]
    
then
        UNIX95
ps -o pid,user,state,comm -p $SLAY_PPID | \
        
awk '$1 ~ /^[0-9]*$/ { print "The program " $4 " with PID " $1 " is being run by the user " $2 " and is currently in state " $3 }'
        
read -"Are you sure you want to kill PID $SLAY_PPID ? Y|N : " COMMIT_KILL
        
if [ "$COMMIT_KILL"Y" ] || [ "$COMMIT_KILL"y" ]
        
then
            kill 
-9 $SLAY_PPID
            
echo ""
            
read -"Killed PID $SLAY_PPID. Run again? Y/N : " GO_AGAIN
        
else
            
Z_Kill
        fi
    
else
        echo 
"Invalid PID. Try again."
        
echo ""
        
Z_Kill
    fi
}

function 
help
{
    echo 
"Usage: zombie_slayer [-h | help -d]"
    
echo "-d just display zombie processes and exit"
    
echo "-h | help : This help"
    
exit
}

if [ 
"$1" "-h" ] || [ "$1" "help" ]
then
    help
fi

if [ "$1" "-d" ]
then
    Z_Display
else
    
Z_Display
    Z_Kill
fi
if [ "$GO_AGAIN"Y" ] || [ "$GO_AGAIN"y" ]
then
    Z_Display
    Z_Kill
fi
echo ""






You would have to paste that into a ssh / bash script and then execute it with   sh myfilename.sh      replace myfilename    with whatever you called the file you saved it to.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How To Help Protect Against DNS Reflection DDoS

More and more each day DNS servers are getting exploited and abused.  Hackers able to use...

How can i cancel my dedicated server?

How Do I Cancel My Dedicated Server & Account? To cancel your account you need to login to your...

How can I move a xen LVM based virtual machine from one server to another?

For those running servers with no back-end SAN, the following instructions detail the steps...

How can i show my cpu info (linux)?

How can i show my cpu info? Log in to your server via SSH then typ;e the following command...

How can i sync hardware clock

How can i sync the hardware clock in cent os?   First make sure your clock is accurate you can...