Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to kill a java process based on the command name... Whats the best way to do it?

(i.e. when you do ps -f the name in the CMD column).

share|improve this question
Btw it's never a good idea to kill a process by his name, because some processes can lie about their names or you can kill the wrong one (especially with killall) by mistake. So if you plan to do that through a script I would recommend to try and get the PID from the java process. Especially if you developed the code just write the PID assigned to the process in one file, which you can use later on as an reference to terminate it. – golja Jul 11 '12 at 1:46 (this is a joke) – The Unix Janitor Jul 16 '12 at 12:50
up vote 11 down vote accepted

Simples, use pkill

pgrep, pkill - look up or signal processes based on name and other attributes

share|improve this answer
Use the "-f" option in pgrep/pkill to match on the full command line, rather than just the process name (which will likely be just "java", so there may be an issue if there are other java processes running). – cjc Jul 9 '12 at 17:57
@cjc the -f parameter is what I was looking for...this should be the answer – DD. Jul 10 '12 at 7:56
It might be unwise to kill something just based on the -f parameter, as root. Perhaps expand it with the -u to ensure you at least have the correct user. – Sirch Jul 11 '12 at 7:45

One way is with killall:

killall - kill processes by name
share|improve this answer
Be VERY careful where you type killall - On Linux (every distro I've encountered), FreeBSD, etc. it kills processes with a given name. On Solaris, HP-UX, and others it kills ALL running processes. Double-check the man pages on the system you're using to avoid unpleasant surprises. – voretaq7 Jul 9 '12 at 16:57
I did this on a production HP-UX box - fortunately, it exits immediately the message 'This command must be run from the root directory'. That prompted me to take a peek at the man page - I'm glad I did! – AlexWebr Jul 9 '12 at 21:57
This is the process name not the command name – DD. Jul 10 '12 at 7:56

You can do it with killall NAME.

Also, you can use the option

-e,--exact          require exact match for very long names
share|improve this answer

If you want a quick script that will kill it in one line, try this:

kill `ps aux | awk '$1 ~ "java" {print $2}'`

Where "java" is in quotes, make sure you change it to whatever name Java is running under. You can check that by running ps aux

If it won't die, you can use kill -9 instead, which ensures execution.

kill -9 `ps aux | awk '$1 ~ "java" {print $2}'`
share|improve this answer
kill -9 does not continue sending kill until the process dies. It sends a signal which cannot be caught, blocked, or ignored by the process. In some cases (typically file or network IO) the process table entry may persist, but the process itself will then be a zombie (you can't kill it, it's already dead). – Dr. Edward Morbius Jul 9 '12 at 20:13
Thanks for the clarification. – nojak Jul 10 '12 at 21:59

Your Answer


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.