Matt Fiddles

Life's so vast, there's just so much to do...

User Tools

Site Tools


Sidebar

Luke Skywalker: "Got 'im! I got 'im!"

Han Solo: "Great, kid! Now don't get cocky!"



Where will you go today?

"Arthur yawed wildly as his skin tried to jump one way and his skeleton the other, whilst his brain tried to work out which of his ears it most wanted to crawl out of.
`Bet you weren't expecting to see me again,' said the monster, which Arthur couldn't help thinking was a strange remark for it to make, seeing as he had never met the creature before. He could tell that he hadn't met the creature before from the simple fact that he was able to sleep at nights."

- Arthur discovering who had diverted him from going to a party.
computers:linux:zombie-slayer

Zombie Slayer

Just a cool little script to slay zombie processes. Or those dumb things that just won't die!

I did a few fixes to make it work better. Originally found here. GDB debugger needs to be installed and you need to be able to have permissions to interact with the process (not that you want to - it's a zombie for crying out loud!).

Output is somthing like this

***********************************************************
This script will terminate all zombie process.
Press [ENTER] to continue or [CTRL] + C to cancel:
***********************************************************
 
***********************************************************
Creating command file...
***********************************************************
possible parentid: 1
possible zombieid: 3163
verifyzombie: Sl                    <-- not a zombie
 
possible parentid: 9259
possible zombieid: 9263
verifyzombie: S+                    <-- not a zombie
 
possible parentid: 3954
possible zombieid: 3975
verifyzombie: Zs                    <-- a zombie
Logging: Parent: 3954 Zombie: 3975
 
attach 3954
call waitpid (3975,0,0)
 
***********************************************************
Slaying zombie processes...
***********************************************************
 
[[gdb output]]
$1 = 3975                   <-- killed the zombie!
 
***********************************************************
Script complete."
***********************************************************

The script:

zombie-slayer
#!/bin/bash
##################################################################
# Script: Zombie Slayer
# Author: Mitch Milner
# Date: 03/13/2013 ---> A good day to slay zombies
#
# Requirements: yum install gdb
# permissions to attach to the parent process
#
# This script works by using a debugger to
# attach to the parent process and then issuing
# a waitpid to the dead zombie. This will not kill
# the living parent process.
##################################################################
 
clear
# Wait for user input to proceed, give user a chance to cancel script
echo "***********************************************************"
echo -e "This script will terminate all zombie process."
echo -e "Press [ENTER] to continue or [CTRL] + C to cancel:"
echo "***********************************************************"
read cmd_string
echo -e "\n"
 
# initialize variables
intcount=0
lastparentid=0
 
# remove old gdb command file
rm -f /tmp/zombie_slayer.txt
 
# create the gdb command file
echo "***********************************************************"
echo "Creating command file..."
echo "***********************************************************"
ps -e -o ppid,pid,stat,command | grep Z | sort | while read LINE; do
intcount=$((intcount+1))
parentid=`echo $LINE | awk '{print $1}'`
zombieid=`echo $LINE | awk '{print $2}'`
verifyzombie=`echo $LINE | awk '{print $3}'`
 
echo "possible parentid: $parentid"
echo "possible zombieid: $zombieid"
echo "verifyzombie: $verifyzombie (this should be Z or simular)"
echo
 
# make sure this is a zombie file and we are not getting a Z from
# the command field of the ps -e -o ppid,pid,stat,command
if [ "$verifyzombie" == "Z" ] || [ "$verifyzombie" == "Zs" ] ; then
 
  if [ "$parentid" != "$lastparentid" ] ; then
 
    if [ "$lastparentid" != "0" ] ; then
 
      echo "detach" >> /tmp/zombie_slayer.txt
 
    fi
 
    echo "attach $parentid" >> /tmp/zombie_slayer.txt
 
  fi
 
  # sometimes useful
  echo "set unwindonsignal on"  >> /tmp/zombie_slayer.txt
 
  echo "call waitpid ($zombieid,0,0)" >> /tmp/zombie_slayer.txt
  echo "Logging: Parent: $parentid Zombie: $zombieid"
  lastparentid=$parentid
fi
#cat /tmp/zombie_slayer.txt
done
if [ "$lastparentid" != "0" ]
then
echo "detach" >> /tmp/zombie_slayer.txt
fi
 
if ! [ -f /tmp/zombie_slayer.txt ] ; then
  echo "No zombies were located..."
  exit 1
fi
 
cat /tmp/zombie_slayer.txt
read "Hit enter to Proceed..."
 
# Slay the zombies with gdb and the created command file
echo -e "\n\n"
echo "***********************************************************"
echo "Slaying zombie processes..."
echo "***********************************************************"
gdb -batch -x /tmp/zombie_slayer.txt
echo -e "\n\n"
echo "***********************************************************"
echo "Script complete."
echo "***********************************************************"
 
rm -f /tmp/zombie_slayer.txt
computers/linux/zombie-slayer.txt · Last modified: Apr 27, 2014 (4 years ago) by Matt Bagley