Kill process by name (or: what pkill already does)

Whenever I wanted to terminate a program by force I used ps -A | grep PROGNAME, read the process id and then executed sudo kill PID. Now I wrote a one line bash script making things easier — reducing the required effort by half:

#!/bin/bash
sudo kill `ps -A | grep $1 | grep -Eo '[0-9]{3,4}'`

Also usable as a one line command which requires one to enter the program’s name afterwards:

read x;sudo kill `ps -A | grep $x | grep -Eo '[0-9]{3,4}'`

The first version, however, is much more convenient. I stored it as nkill in /usr/bin/ which enables me to for example kill firefox with nkill firefox or kill all open terminals with nkill xterm.

Update: Uhm … yeah … I just learned that pkill does just that. :D

2012-01-24