Kill a Java Process (In Linux) by Process Name Instead of Pid

Kill a java process (in linux) by process name instead of PID

Here is the command to kill the Java process by is Process Name instead of its ProcessID.

kill -9 `jps | grep "DataNode" | cut -d " " -f 1`

Let me explain more, about the benefit of this command. Lets say you are working with Hadoop cluster. Its often required that you check java daemons running with jps command. Lets say when you give this command on worker nodes, you see following output.

1915 NodeManager
18119 DataNode
17680 Jps

Usually, if we want to kill DataNode process, we would use following command

kill -9 18119

But, it is little bit difficult to type the PID, to use kill command. By using the command, given in this answer, it is easy to write the name of the process. We can also prepare shell scripts to kill commonly used deamons in hadoop cluster,
or we can prepare one shell script and can use parameter as process name.

Killing a JVM process using the name instead of PID

Here is the command to kill the Java process by is Process Name instead of its ProcessID.

kill `jps | grep "DataNode" | cut -d " " -f 1`

How can I kill a process by name instead of PID, on Linux?


pkill firefox

More information: http://linux.about.com/library/cmd/blcmdl1_pkill.htm

Kill a java process using its Name and not PID

Finally found a easy solution here

jps - Java Virtual Machine Process Status Tool , can be used for killing a java process with its name.

for /f "tokens=1" %i in ('C:\java\jdk-7u45-windows-x64\bin\jps -m ^| find "DummyBroker"') do ( taskkill /F /PID %i )

We can add above in batch file and simply call the batch file from java program.

How can I kill a process from a Java application, if I know the PID of this process? I am looking for a cross-platfrom solution

Since Java 9 there's ProcessHandle which allows interaction with all processes on a system (constrained by system permissions of course).

Specifically ProcessHandle.of(knownPid) will return the ProcessHandle for a given PID (technically an Optional which may be empty if no process was found) and destroy or destroyForcibly will attempt to kill the process.

I.e.

long pid = getThePidViaSomeWay();
Optional<ProcessHandle> maybePh = ProcessHandle.of(pid);
ProcessHandle ph = maybePh.orElseThrow(); // replace with your preferred way to handle no process being found.
ph.destroy(); //or
ph.destroyForcibly(); // if you want to be more "forcible" about it

How to kill all processes with a given partial name?

Use pkill -f, which matches the pattern for any part of the command line

pkill -f my_pattern

Just in case it doesn't work, try to use this one as well:

pkill -9 -f my_pattern

linux script to kill java process

You can simply use pkill -f like this:

pkill -f 'java -jar'

EDIT: To kill a particular java process running your specific jar use this regex based pkill command:

pkill -f 'java.*lnwskInterface'


Related Topics



Leave a reply



Submit