Kill only one Java process

219,507

Solution 1

For killing a process that is associated with multiple processes, you need to kill that by using process id associated with that process.

To get the process id of that java process run

ps -A |grep java

output of this command will give the list of java processes running on your system. Note down Process ID (PID) of that process whom you want to kill and run

kill -9 PID

Solution 2

IMO the best solution is:

pkill -9 -f <nameOfYourJavaAplication>

Solution 3

You can simply use

jps -l

command to get all of the process id's of java processes and then issue

kill PROCESS_ID

command

Solution 4

If you want a script to find and kill your (specific) running program using a pattern from the command line of the program (and not just every java thing you got running), then:

pgrep -f pattern # shows the PID
pkill -f pattern

From https://unix.stackexchange.com/a/31111/249209

Share:
219,507

Related videos on Youtube

Petr Mensik
Author by

Petr Mensik

I mostly do Java and Java EE stuff although I am currently also interested in security and big data. SOreadytohelp

Updated on September 18, 2022

Comments

  • Petr Mensik
    Petr Mensik over 1 year

    I usually run few Java applications, one for server running locally and other for some IDE like NetBeans. And from time to time, after lots of redeployments, my server get stuck on OutOfMemoryException so I need to kill Java process in order to reboot.

    So I do pkill -9 java but this also kills my running IDE which I don't want to. So how do I kill only application linked to running server and not the other ones?I assume that they all are running under same process but there has to be some way how to distuingish them.

    • Admin
      Admin over 11 years
      look at your process table (via top or ps) and choose the right one and kill it by PID (kill -9 PID_number).
    • Admin
      Admin over 11 years
      Oh yes, I can see that now. So I just need to kill process with right PID. Thanks a lot
    • Admin
      Admin about 5 years
      I need to automate this. How to get the PID for the particular program?
  • Andrii Karaivanskyi
    Andrii Karaivanskyi over 6 years
    If you want to kill ALL java processes by one command ps ax | grep java | grep -v 'grep' | cut -d '?' -f1 | xargs kill -9
  • Alexis Wilke
    Alexis Wilke over 5 years
    Those only list themselves on my computer.
  • Muhammad Gelbana
    Muhammad Gelbana over 5 years
    Then, are you sure there are other Java processes running ?!
  • Alexis Wilke
    Alexis Wilke over 5 years
    Yes. I had Cassandra or Elassadra running when testing your command.
  • cactuschibre
    cactuschibre over 5 years
    Perfect. I used ps -fC java to find nameOfYourJavaAplication (in -Dprogram.name)
  • Jeff Schaller
    Jeff Schaller about 5 years
    Notice that "pkill" was already mentioned in Marek R's answer of Feb 13 '13