35

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.

qbi
  • 18,879
  • 9
  • 79
  • 127
mreq
  • 4,752
  • 8
  • 42
  • 62
  • 3
    You can use `-9` with `pkill` too... – OrangeDog Jan 11 '13 at 17:28
  • 6
    In my opinion, it's more elegant and proper to use `-KILL` than `-9`. What number corresponds to what signal is implementation-dependent. `SIGKILL` happens to be `9` on Linux i386 and Linux amd64, but not necessarily everywhere. ([More info here.](http://en.wikipedia.org/wiki/Kill_-9#Unix_and_Unix-like)) – Eliah Kagan Jan 11 '13 at 23:36
  • 6
    @EliahKagan: while I like to use `-KILL` too (because it makes code more readable), it should be noted that `SIGKILL = 9` is [specified by POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/kill.html), so `-9` is pretty portable nowadays (and does not depend on kernels or architectures). – Andrea Corbellini Jan 17 '13 at 18:51

6 Answers6

58

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.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
Gerhard Burger
  • 9,359
  • 5
  • 41
  • 59
39

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).

qbi
  • 18,879
  • 9
  • 79
  • 127
16

The other answers are entirely correct, but I might as well add a third option so all are documented here. You can also use:

pkill -9 middleman

See man pkill for more information and extra options.

It doesn't really matter which of these methods you use. They will all work. But knowing the options if useful if you want to modify the behaviour in some way, since the corresponding man pages show what other matching options are available.

Robie Basak
  • 15,524
  • 4
  • 61
  • 86
  • Strange, doesn't work here. Nor does `middleman*` – mreq Jan 11 '13 at 20:47
  • 1
    Ah. Looking more closely, it seems likely that you're wanting to kill a ruby interpreter, rather than a process really called `middleman`. This is where the differences in how exactly `pkill`, `killall`, `pidof` and `grep` match processes really start to matter! – Robie Basak Jan 11 '13 at 20:50
14

Just for fun, I'd like to add a more manual, old school way

kill -9 `ps aux | grep middleman | awk '{print $2}'`
Gerhard Burger
  • 9,359
  • 5
  • 41
  • 59
slipset
  • 241
  • 1
  • 3
  • 1
    I did that at work a few years ago and the admin gave me a look that said "okay, grandad". Can also add a grep -v grep before the awk to make sure it doesn't kill the grep command too. – Iain Holder Jan 12 '13 at 08:16
  • @gerhard, thanks for the edit, couldn't find the back ticks on my iPad. grep -v is a good idea... – slipset Jan 12 '13 at 08:23
  • The real old school way would be to write `... grep [m]iddleman ...` to avoid killing the `grep` process by accident. That way the grep cannot match itself because of funny quoting. – Mikko Rantalainen Jan 13 '16 at 13:13
14
pkill -9 -f middleman

The -f option makes it match the complete command line, rather than only the process name.

Note that -9 should be a last resort signal, not something to use routinely.

gertvdijk
  • 67,007
  • 33
  • 188
  • 283
jlliagre
  • 5,693
  • 1
  • 24
  • 28
1
killall -9 -ir regex_pattern

which is interactive(safe) and matches the partial command name.

Bohr
  • 221
  • 2
  • 4