Matt Fiddles

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

User Tools

Site Tools


Sidebar

"The young man knows the rules, but the old man knows the exceptions."



Where will you go today?

R2-D2 and Chewbacca are playing the holographic game aboard the Millennium Falcon.

Chewbacca: "Aaaaaaaaaaaaaaaarrrgh"

C-3PO: "He made a fair move. Screaming about it can't help you."

Han Solo: "Let him have it. It's not wise to upset a Wookiee."

C-3PO: "But sir, nobody worries about upsetting a droid."

Han Solo: "That's 'cause droids don't pull people's arms out of their sockets when they lose. Wookiees are known to do that."

Chewbacca: "Grrf."

C-3PO: "I see your point, sir. I suggest a new strategy, R2, let the Wookiee win."
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 (5 years ago) by Matt Bagley