#!/bin/sh # die - a script to kill most unruly programs most of the times. See disclaimers # RTH 07/29/04 - First try. Works 90% of the time # RTH 09/28/07 - Fixed many annoyances. Coding while lightly buzzed, caveat emptor # Disclaimers: # - Anything other than a y/n or Y/N will exit without error. Need another case statement # - It doesn't deal with multiple instances of a given program, and ignores defunct/zombie # processes. Should just be a loop/array setup though, easy to implement # General notes: # grep -v = find all things NOT patching the "pattern" # `execute everything in this quotes first` # Pipes are your friends! # exit 1 bails, sort of like a return -1 in C # Error checking, bail if no arguments given (otherwise would kill our terminal!) if test $# = 0 then echo "You didn't tell me what to kill!" exit 1 fi # This next bit removes all references of $1 in our script - awk and grep and even /bin/sh will show up # in the list without filtering them out like this. You could also do this in sed, or just about any other # UNIX filter old skool style. This is all for safety sake, strictly speaking the first instance showing up # should be the one with the lowest PID and the one started earliest on the system. But this uses more # functions and stuff :) # Nifty trick - note how you can jump in and out of the awk environment to get at my input variable $1 pidder=`ps -ef | grep $USER | grep $1 | awk '{if ($8~'$1') {print $0} }' | \\ grep -v "/bin/sh" | grep -v "awk" | grep -v "grep" | grep -v "defunct"` if `echo $pidder | grep -qv $1` then echo "Sorry, nothing called \"$1\" was found running. Perhaps another name?"; exit 1 fi echo "Killing: $pidder" read -p "Really do it? (y/n) " answer ans=`echo $answer | tr [A-Z] [a-z]` case $ans in \y) kill -9 `echo $pidder | awk '{print $2}'` ;; \n) exit 1 ;; esac