Kill only one Java process
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
Related videos on Youtube
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, 2022Comments
-
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 over 11 yearslook at your process table (via
top
orps
) and choose the right one and kill it by PID (kill -9 PID_number). -
Admin over 11 yearsOh yes, I can see that now. So I just need to kill process with right
PID
. Thanks a lot -
Admin about 5 yearsI need to automate this. How to get the PID for the particular program?
-
-
Andrii Karaivanskyi over 6 yearsIf 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 over 5 yearsThose only list themselves on my computer.
-
Muhammad Gelbana over 5 yearsThen, are you sure there are other Java processes running ?!
-
Alexis Wilke over 5 yearsYes. I had Cassandra or Elassadra running when testing your command.
-
cactuschibre over 5 yearsPerfect. I used
ps -fC java
to findnameOfYourJavaAplication
(in-Dprogram.name
) -
Jeff Schaller about 5 yearsNotice that "pkill" was already mentioned in Marek R's answer of Feb 13 '13