: ########################################################################## # Shellscript: killn - kill process by name # Version : 0.6 (beta) # Author : Heiner Steven (heiner.steven@odn.de) # Date : 29.03.95 # SCCS-Id. : $Id: killn,v 1.1.1.1 1999/06/15 19:29:05 heiner Exp $ ########################################################################## # Notes # This script is tailored to the BSD (SunOs 4.1.3) ps command. # Changes # 05.02.96 stv Test "ps" arguments (0.5) # 14.02.96 stv Handle signal number (0.6) ########################################################################## PN=`basename $0` # program name VER='0.6 (beta)' # Grace period to wait after "kill" for process to terminate Grace=3 if ps -ax >/dev/null 2>&1 then PsDefault=ax # Arguments to "ps" PIDCol=1 # Column number of PID else PsDefault=ef PIDCol=2 fi Usage () { echo "$PN - kill process by name, $VER (hs '95) usage: $PN [-signal] [-psargs] name [name ...] -signal: signal number (default is first 15, then 9) -psargs: arguments to the 'ps'-command name is any string matching the 'ps' output. There is no way to kill a command named $PN. The default ps command is ps -$PsDefault." >&2 exit 1 } Msg () { echo "$PN: $*" >&2; } Fatal () { Msg "$@"; exit 1; } # Collect Parameters for ps while [ $# -gt 0 ] do case "$1" in --) shift; break;; -[1-9]|-[1-3][0-9]) # Signal number Sig=`expr "$1" : '-\(.*\)'` ;; -h) Usage;; # Special Handling for BSD ps: -u) ;; -t) PsOpt="${PsOpt}t$2";; -*) PsOpt="${PsOpt}`echo "$1" | sed 's:^-::'`";; *) break;; # First process name esac shift done [ $# -lt 1 ] && Usage : ${PsOpt:=$PsDefault} # Default for ps PsOpt="-${PsOpt}" # Show real command name # Bring process names in a form suitable for awk: # "one two tree" -> "[o]ne|[t]wo|[t]hree" # The square brackerts assert that awk does not match itself. Search= for i do Search="${Search:+$Search|}`echo "$i" | sed -e 's:^\(.\):\[\1\]:'`" done PIDS=`ps "$PsOpt" | awk '($0 ~ /'"$Search"'/ && $0 !~ /'"$PN"'/) {print $'$PIDCol'}'` [ -z "$PIDS" ] && Fatal "no matching processes found" Msg ${Sig:+signal $Sig, }pids: $PIDS if expr "$Sig" + 0 > /dev/null 2>&1 then # Signal explicitely given: don't send SIGKILL echo $PIDS | xargs kill -"$Sig" else (echo $PIDS | xargs kill sleep ${Grace:-1} echo $PIDS | xargs kill -9 > /dev/null 2>&1)& fi