Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 11 down vote accepted

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.

share|improve this answer
    
+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

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.

share|improve this answer
    
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

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`
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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