#!/bin/sh ######################################################################### # # FILE : killbyname # DATE : 2.8.2001 # AUTHOR : Patrick Meier / patrick.meier@gmx.net # DESCRIPTION : kill process by given name # # Copyrigth (C) 2001 by Patrick Meier, Switzerland # ######################################################################### PN=`basename "$0"` STARTSTRING="==>" ######################################################################### # Usage ######################################################################### Usage() { echo "$PN - kill process by given name" echo "usage: $PN name1 name2..." } ######################################################################### # readText ######################################################################### readText() { message="${STARTSTRING}$@" answer="" while [ -z "$answer" ] do printf "$message" read answer done } ######################################################################### # killProcess ######################################################################### killProcess() { KILL="kill" for i in $@ do a=1 cmd="$KILL -$a $i" printf "${STARTSTRING}killing process $i..." while ( ! eval $cmd >/dev/null 2>&1) do if [ $a -lt 9 ] then ((a=a+1)) cmd="$KILL -$a $i" else break; fi done if [ $a -lt 9 ] then printf "ok.\n" else printf "failed. Can not kill process $i!\n" fi done } ######################################################################### # killByName ######################################################################### killByName() { if [ -z "$1" ] then return fi printf "${STARTSTRING}kill all '$i' process..." entries="`ps -ef | grep \"$1\" | grep -v \"grep\" | grep -v \"$PN\"`" process="`echo \"$entries\" | awk '{print $2}'`" if [ -z "$process" ] then printf "no process found!\n" return else printf "\n" for i in "$entries" do printf "$i\n" done printf "\n" readText "kill all this processes? (y/n)" if [ "$answer" = "y" ] then killProcess $process else echo "${STARTSTRING}quit" return fi fi } ######################################################################### # main ######################################################################### if [ "$#" -lt 1 ] then Usage exit 1 fi for i in $@ do if [ -n "$i" ] then killByName $i fi done ######################################################################### # EOF #########################################################################