24.16. Kill Processes InteractivelyWhen you want to kill processes, it's a pain in the neck to run ps (Section 24.5), figure out the process ID, and then kill the process -- although sometimes you have to do it that way (Section 24.15). We'll look at two easier ways. 24.16.1. killall -iMany systems have a command named killall with a -i ("interactive") option. Be careful, though, because there are several versions, and the most basic does just what it says: kills all processes on the system (when run as the superuser (Section 1.18)). Check killall's manual page on your system. The version of killall we're talking about here accepts multiple process-name arguments on its command line. Without its -i option, the command sends a signal (by default, TERM) to any process name that matches. The process name you give has to match completely. Unfortunately, killall sends a signal to any process with that name -- even processes owned by other users, which you can't kill (unless you're the superuser); you'll get the error Operation not permitted. For example: & Section 23.2, [5] Section 23.3 1$ cruncher & sleep 60 & [5] 2714 [6] 2715 $ killall crunch eep crunch: no process killed eep: no process killed $ killall cruncher sleep sleep(2708): Operation not permitted sleep(2710): Operation not permitted sleep(2712): Operation not permitted [5] Terminated cruncher [6] Terminated sleep 60 With -i, cruncher lists the PID number and gives you a choice of typing y to kill a process or n to leave it alone: $ cruncher & sleep 60 & [5] 2732 [6] 2733 $ killall -i cruncher sleep Kill sleep(2727) ? (y/n) y sleep(2727): Operation not permitted Kill cruncher(2732) ? (y/n) y Kill sleep(2733) ? (y/n) y Kill sleep(2734) ? (y/n) n [5] Terminated cruncher [6] Terminated sleep 60 24.16.2. zapA more flexible way to kill processes interactively is the zap shell script, presented by Brian Kernighan and Rob Pike in their classic book The UNIX Programming Environment. The script uses egrep (Section 13.4) to pick the processes to kill; you can type extended expressions (Section 32.15) that match more than one process. The expressions can match partial or complete command names, any arguments to the commands, or, actually, any part of the command's line in the ps output. For example: % zap 'troff|fmat' PID TTY TIME CMD 22117 01 0:02 fmat somefile? n 22126 01 0:15 sqtroff -ms somefile? y We reprint the script by permission of the authors: Go to http://examples.oreilly.com/upt3 for more information on: zap '...' Section 36.24 #! /bin/sh # zap pattern: kill all processes matching pattern PATH=/bin:/usr/bin IFS=' ' # just a newline case $1 in "") echo 'Usage: zap [-2] pattern' 1>&2; exit 1 ;; -*) SIG=$1; shift esac echo ' PID TTY TIME CMD' kill $SIG `pick \`ps -ag | egrep "$*"\` | awk '{print $1}'` The ps -ag command displays all processes on the system. Leave off the a to get just your processes. Your version of ps may need different options (Section 24.5). This shell version of zap calls another script, pick, shown below.[75] pick shows each of its command-line arguments and waits for you to type y, q, or anything else. Answering y writes the line to standard output, answering q aborts pick without showing more lines, and any other answer shows the next input line without printing the current one. zap uses awk (Section 20.10) to print the first argument (the process ID number) from any ps line you've selected with pick. The inner set of nested (Section 36.24) backquotes (Section 28.14) in zap pass pick the output of ps, filtered through egrep. Because the zap script has set the IFS variable (Section 36.23) to just a newline, pick gets and displays each line of ps output as a single argument. The outer set of backquotes passes kill (Section 24.12) the output of pick, filtered through awk.
If you're interested in shell programming and that explanation wasn't detailed enough, take a careful look at the scripts -- they're really worth studying. (This book's shell programming chapters, 44 through 46, may help, too.) Here's the pick script: Go to http://examples.oreilly.com/upt3 for more information on: pick /dev/tty Section 36.15 #!/bin/sh # pick: select arguments PATH=/bin:/usr/bin for i do echo -n "$i? " >/dev/tty read response case $response in y*) echo $i ;; q*) break esac done </dev/tty -- JP Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|