5

I am using the following command to kill one java process via shell script:

pidof java|xargs kill -9 $1

What if the list of pidof output has more than one elements? How do I kill them both with the same one command?

13

First of all, check out pkill. You can kill off any number of process given their name:

pkill java

You can even use the full command with arguments as part of the search

pkill -f some_string_in_arguemnts

Secondly, your construct with xargs will work just fine for multiple PID's as long as they are piped in as either space or newline separated numbers.

  • +1: Thanks for fast answer. Kill with several process ids seems to be the best suitable solution for my purposes. – mico Aug 25 '11 at 10:37
  • These utils (pkill, pgrep, pfind) aren't available on MacOS (at least not 10.5.x). If you're using MacPorts, the proctools package supplies them. – Pat Notz Aug 29 '11 at 17:41
6

The killall command is available on most Linuces at any rate. You might have to install it on some distributions. It allows you to kill all processes that match the name provided on the command line.

  • But beware if you are on a different system. Standard (non-GNU) killall does just that: it kills all processes (especially effective when executed by root). I've managed to bring down a Solaris and a Digital machine in this way when my fingers worked faster than my brain. – Ansgar Esztermann Jan 26 '12 at 9:36
3

pgrep can give you the list of PIDs

For example:

pgrep java 

will give you the list of PIDs related to java. So you can use:

kill -9 `pgrep java`
0

I prefer

kill -9 `pidof java`

because of the inline output

> pgrep gulp
6316
6565
> pidof gulp
6565 6316

The problem with pkill, well you don't have it installed by default on some systems (like Ubuntu)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.