Shell script to -9 kill based on name

Is there a way (perhaps a script) how to automate this process:

petr@sova:~$ ps -ef | grep middleman
petr     18445  2312  1 12:06 pts/2    00:00:01 /home/petr/.rvm/gems/ruby-1.9.3-p362/bin/middleman                                                                  
petr     18581 13621  0 12:08 pts/0    00:00:00 grep --color=auto middleman
petr@sova:~$ kill -9 18445

Unfortunately, pkill is too weak as I have to go with -9 option on kill.


Solution 1:

There is an even simpler solution than the one of qbi: killall let's you kill processes by name, and you can specify signals.

killall -9 middleman

See man killall for more information and extra options (there are quite a few).

As the name suggests, this does send the signal to all processes named middleman. But that's not different from other ways (like pkill). Furthermore, pkill -9 middleman will target processes whose name match but do not equal middleman, such as middleman2, as well.

Solution 2:

You can use your shell to do this task for you:

kill -9 $(pidof middleman)

The shell executes the command pidof middleman first. The output of pidof(8) is the process id. So the shell substitutes the pidof-command with the process id and executes kill -9 18845 (or whatever the correct process id is).