| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # kill-byname.sh: Killing processes by name. |
| 3 | # Compare this script with kill-process.sh. |
| 4 | |
| 5 | # For instance, |
| 6 | #+ try "./kill-byname.sh xterm" -- |
| 7 | #+ and watch all the xterms on your desktop disappear. |
| 8 | |
| 9 | # Warning: |
| 10 | # ------- |
| 11 | # This is a fairly dangerous script. |
| 12 | # Running it carelessly (especially as root) |
| 13 | #+ can cause data loss and other undesirable effects. |
| 14 | |
| 15 | E_BADARGS=66 |
| 16 | |
| 17 | if testscip-shell system . . test. -z "$1" # No command-line arg supplied? |
| 18 | then |
| 19 | echoscip-shell system . . echo. "Usage: `basenamescip-shell system . . basename. $0` Process(es)_to_kill" |
| 20 | exit $E_BADARGS |
| 21 | fi |
| 22 | |
| 23 | |
| 24 | PROCESS_NAME="$1" |
| 25 | psscip-shell system . . ps. ax | grepscip-shell system . . grep. "$PROCESS_NAME" | awkscip-shell system . . awk. '{print $1}' | xargsscip-shell system . . xargs. -i kill {} 2&>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 26 | # ^^ ^^ |
| 27 | |
| 28 | # --------------------------------------------------------------- |
| 29 | # Notes: |
| 30 | # -i is the "replace strings" option to xargs. |
| 31 | # The curly brackets are the placeholder for the replacement. |
| 32 | # 2&>/dev/null suppresses unwanted error messages. |
| 33 | # |
| 34 | # Can grep "$PROCESS_NAME" be replaced by pidof "$PROCESS_NAME"? |
| 35 | # --------------------------------------------------------------- |
| 36 | |
| 37 | exit $? |
| 38 | |
| 39 | # The "killall" command has the same effect as this script, |
| 40 | #+ but using it is not quite as educational. |