A bash script to kill a process

Jan 11, '03 09:57:25AM

Contributed by: Anonymous

I was talking to my Unix friend about killing apps easily, from the Terminal. This is what we came up with. Just slide it into your .bashrc, and you'll be able to killapp brokenapp!

 function psapp() {
ps -ax | grep -i $1 | grep -i -v "grep.-i.$1" | awk '{print $1}'
}
function killapp() {
kill $(psapp $1)
}
The first function lists the process id for the argument that you request. So psapp word will list the process id for word.

Breaking it down, ps -ax lists the processes, piping to grep -i lists just the lines that have the argument you entered. Unfortunately, it also lists grep and the argument, since that's a running process as well. Therefore, the second grep excludes the grep process. Finally, awk parses for the first column. The second function, killapp, takes the process id as an argument and kills the running process.

To kill an app, just type 'killapp appname' ... or partial app name. It's case insensitive to boot!

[Editor's note: I don't have the bash shell, so I haven't tested this one.]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030111065725195