LDP/LDP/guide/docbook/abs-guide/kill-byname.sh

41 lines
1.1 KiB
Bash
Raw Normal View History

2004-02-16 17:21:33 +00:00
#!/bin/bash
# kill-byname.sh: Killing processes by name.
# Compare this script with kill-process.sh.
# For instance,
#+ try "./kill-byname.sh xterm" --
#+ and watch all the xterms on your desktop disappear.
# Warning:
# -------
# This is a fairly dangerous script.
# Running it carelessly (especially as root)
#+ can cause data loss and other undesirable effects.
E_BADARGS=66
2008-11-23 22:43:47 +00:00
if test -z "$1" # No command-line arg supplied?
2004-02-16 17:21:33 +00:00
then
echo "Usage: `basename $0` Process(es)_to_kill"
exit $E_BADARGS
fi
2004-03-15 13:47:54 +00:00
PROCESS_NAME="$1"
ps ax | grep "$PROCESS_NAME" | awk '{print $1}' | xargs -i kill {} 2&>/dev/null
2004-03-15 13:47:54 +00:00
# ^^ ^^
2007-04-26 21:13:49 +00:00
# ---------------------------------------------------------------
2004-02-16 17:21:33 +00:00
# Notes:
# -i is the "replace strings" option to xargs.
2004-03-15 13:47:54 +00:00
# The curly brackets are the placeholder for the replacement.
# 2&>/dev/null suppresses unwanted error messages.
2007-04-26 21:13:49 +00:00
#
# Can grep "$PROCESS_NAME" be replaced by pidof "$PROCESS_NAME"?
# ---------------------------------------------------------------
2004-02-16 17:21:33 +00:00
exit $?
2006-06-22 14:28:13 +00:00
# The "killall" command has the same effect as this script,
#+ but using it is not quite as educational.