UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 38.12 Killing All Your Processes Chapter 38
Starting, Stopping, and Killing Processes
Next: 38.14 Processes Out of Control?  Just STOP Them
 

38.13 Interactively Kill Processes Matching a Pattern

When you want to kill processes, it's a pain in the neck to run ps (38.5), figure out the process ID, and then kill the process. The zap shell script was presented by Brian Kernighan and Rob Pike in their classic book The UNIX Programming Environment. The script uses egrep (27.5) to pick the processes to kill; you can type extended expressions that match more than one process, such as:

% zap 'troff|fmat'
   PID TTY TIME CMD
 22117  01 0:02 fmat somefile? n
 22126  01 0:15 sqtroff -ms somefile? y

We've reprinted the script by permission of the authors:













`...` 

#! /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 (38.5).

This shell version of zap calls another script, pick, shown below. [6] 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 (33.11) to print the first argument (the process ID number) from any ps line you've selected with pick. The inner set of nested (45.31) backquotes (9.16) in zap pass pick the output of ps, filtered through egrep. Because the zap script has set the IFS variable (35.21) to just a newline, pick gets and displays each line of ps output as a single argument. The outer set of backquotes passes kill (38.10) the output of pick, filtered through awk.

[6] The MH mail system also has a command named pick. If you use MH, you could rename this script to something like choose.

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:








-n /dev/tty 





done < 

#!/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


Previous: 38.12 Killing All Your Processes UNIX Power ToolsNext: 38.14 Processes Out of Control?  Just STOP Them
38.12 Killing All Your Processes Book Index38.14 Processes Out of Control? Just STOP Them

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System