mariusv.com

Watch as I awkwardly stumble through life

Automatic "zombie" processes killing (shell script)

Posted by Marius Voila on February 23, 2010 in London, U.K . — 0 comments This post contains 438 words

Here is a shell script example that can be run by cron for automatic killing of zombie processes in Linux system:

    #!/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
    

or

Download: zombies.sh