24

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.

8 Answers8

33

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
manatwork
  • 31,277
ifexploit
  • 661
18

IMO the best solution is:

pkill -9 -f <nameOfYourJavaAplication>
Marek R
  • 293
11

Instead of using ps and grep, you can use ps's -C flag to select all commands listed with the name 'java'. You may also want to use ps's -f flag to print the full command name of each listed process. That way, you can see what each java process is actually doing. Here is the command in full: ps -fC java.

You could also use pgrep to list all java processes. pgrep -a java will return the PID and full command line of each java process.

Once you have the PID of the command you wish to kill, use kill with the -9 (SIGKILL) flag and the PID of the java process you wish to kill. Java doesn't always stop when it receives a 'SIGTERM' signal (processes are allowed to handle 'SIGTERM'), so sending it the 'SIGKILL' signal, which makes init kill the program without warning it first, is often necessary.

For example, if ps -fC java returns

UID        PID  PPID  C STIME TTY          TIME CMD
jeff      9014  8890  0 08:51 pts/0    00:00:00 java IDE
jeff     11775  8890  6 08:59 pts/0    00:00:00 java TestProgram

or psgrep -a java returns

9014 java IDE
11775 java TestProgram

and you wish to kill java TestProgram, you should run kill -9 11775.

1

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

Nikhil VJ
  • 361
1

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

AdminBee
  • 22,803
0

jps and jcmd are tools specialized in displaying only Java processes information and PIDs.

You can explore their options for a more detailed output.

Muhammad Gelbana
  • 1,643
  • 8
  • 20
  • 26
0

Here is a script one can use to automate the process.

Replace the <PROCESS_NAME> part with whatever java is executing.

#!/bin/sh
process=`ps -ef | grep -v awk | awk -e '/java.*<PROCESS_NAME>/ { print $2 }'`
kill ${process}

Note: I did not put the -9. I should not be required unless you capture signals and SIGTERM (the default) fails. I would also suggest you use the signal name which makes it easier to read:

kill -TERM ${process}

WARNING

Before executing the kill ... make sure that the $process variable gets set as expected. The ps -ef may different between Unices so awk may need to print a different parameter.

Note

The <PROCESS_NAME> could be changed into a variable. Just remember that if you have to type it on your command line each time, you are likely to make mistakes once in a while. I find it easier to have multiple copy of the script with the correct name.

Also important: notice the single quotes for the awk script. That means you need to close and reopen the string with the variable in between. Something like this should work (untested):

process=`ps -ef | grep -v awk | awk -e '/java.*'"${PROCESS_NAME}"'/ { print $2 }'`
Alexis Wilke
  • 2,857
0

My gradle projects don't call /bin/java directly. They create a symlink based on project name at runtime. In this example ps shows "java_my_app" instead of just "java". You can easily pkill or killall the correct JVM. This also helps identify processes in top, htop etc.

In all my Java projects' build.gradle files I add:

apply from: '../shared.gradle'

~/.gradle/gradle.properties:

org.gradle.java.home=/opt/jdk

settings.gradle:

rootProject.name = 'my_app'

shared.gradle:

import java.nio.file.Files
import java.nio.file.Path

tasks.withType(JavaExec) { def javaExecutablesPath = project.property('org.gradle.java.home') + '/bin' println "javaExecutablesPath=" + javaExecutablesPath Path j1 = Path.of(javaExecutablesPath).resolve("java") Path jLink = Path.of(javaExecutablesPath).resolve("java_" + rootProject.name) if (!Files.exists(j1)){ println j1 + " is missing." } else if (Files.exists(jLink)) { println jLink + " exists, OK." } else { java.nio.file.Path p = Files.createSymbolicLink(jLink, j1) println "created " + p } executable = jLink.toFile() }

Andrew
  • 101