6

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

reinierpost
  • 412
  • 3
  • 9
DD.
  • 3,114
  • 11
  • 35
  • 50
  • 1
    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 01:46
  • http://psdoom.sourceforge.net/ (this is a joke) – The Unix Janitor Jul 16 '12 at 12:50

4 Answers4

11

Simples, use pkill

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

Sirch
  • 5,785
  • 4
  • 20
  • 36
  • 2
    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 09 '12 at 17:57
  • @cjc the -f parameter is what I was looking for...this should be the answer – DD. Jul 10 '12 at 07: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 07:45
4

One way is with killall:

killall - kill processes by name
James Sneeringer
  • 6,835
  • 24
  • 27
  • 9
    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 09 '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! –  Jul 09 '12 at 21:57
  • This is the process name not the command name – DD. Jul 10 '12 at 07:56
2

You can do it with killall NAME.

Also, you can use the option

-e,--exact          require exact match for very long names
blacksoul
  • 252
  • 7
  • 22
-1

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}'`
tacotuesday
  • 1,389
  • 1
  • 16
  • 27
  • 1
    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). http://www.kernel.org/doc/man-pages/online/pages/man7/signal.7.html – Dr. Edward Morbius Jul 09 '12 at 20:13