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?

4 Answers 4


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
    Commented Aug 25, 2011 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
    Commented Aug 29, 2011 at 17:41

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. Commented Jan 26, 2012 at 9:36

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`

I prefer

kill -9 `pidof java`

because of the inline output

> pgrep gulp
> pidof gulp
6565 6316

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .