Skip to content

Commit

Permalink
Implement Java 9 process termination
Browse files Browse the repository at this point in the history
Decided to finally use this now that people are updating their JDK versions.

We can never have too many ways to track down and terminate processes around here, can we?
  • Loading branch information
ME1312 committed Aug 1, 2021
1 parent 52fd155 commit 6890b94
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
Expand Up @@ -6,6 +6,7 @@

import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;

/**
* Executable Handler Class
Expand Down Expand Up @@ -93,18 +94,28 @@ public static Long pid(Process process) {
*/
public static void terminate(Process process) {
if (process.isAlive()) {
Long pid = pid(process);
if (pid != null) try {
if (Platform.getSystem() == Platform.WINDOWS) {
Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()}).waitFor();
} else if (USE_SESSION_TRACKING) {
Runtime.getRuntime().exec(new String[]{"bash", "-c", "kill -9 $(ps -s " + pid + " -o pid=)"}).waitFor();
}
} catch (IOException | InterruptedException e) {}
Long pid;
if (Platform.getSystem() == Platform.WINDOWS) {
if ((pid = pid(process)) != null) Util.isException(() -> Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()}).waitFor());
} else if (USE_SESSION_TRACKING) {
if ((pid = pid(process)) != null) Util.isException(() -> Runtime.getRuntime().exec(new String[]{"bash", "-c", "kill -9 $(ps -s " + pid + " -o pid=)"}).waitFor());
}

if (process.isAlive()) {
if (process.isAlive() && terminate9(process)) {
process.destroyForcibly();
}
}
}

private static boolean terminate9(Object handle) {
try { // Attempt iteration over Java 9 ProcessHandle objects
Class<?> clazz = handle.getClass();
Stream<?> children = (Stream<?>) clazz.getMethod("descendants").invoke(handle);
clazz.getMethod("destroyForcibly").invoke(handle);
children.forEach(Executable::terminate9);
return false;
} catch (Throwable e) {
return true;
}
}
}
Expand Up @@ -25,7 +25,6 @@
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;
Expand Down
Expand Up @@ -6,6 +6,7 @@

import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;

/**
* Executable Handler Class
Expand Down Expand Up @@ -93,18 +94,28 @@ public static Long pid(Process process) {
*/
public static void terminate(Process process) {
if (process.isAlive()) {
Long pid = pid(process);
if (pid != null) try {
if (Platform.getSystem() == Platform.WINDOWS) {
Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()}).waitFor();
} else if (USE_SESSION_TRACKING) {
Runtime.getRuntime().exec(new String[]{"bash", "-c", "kill -9 $(ps -s " + pid + " -o pid=)"}).waitFor();
}
} catch (IOException | InterruptedException e) {}
Long pid;
if (Platform.getSystem() == Platform.WINDOWS) {
if ((pid = pid(process)) != null) Util.isException(() -> Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()}).waitFor());
} else if (USE_SESSION_TRACKING) {
if ((pid = pid(process)) != null) Util.isException(() -> Runtime.getRuntime().exec(new String[]{"bash", "-c", "kill -9 $(ps -s " + pid + " -o pid=)"}).waitFor());
}

if (process.isAlive()) {
if (process.isAlive() && terminate9(process)) {
process.destroyForcibly();
}
}
}

private static boolean terminate9(Object handle) {
try { // Attempt iteration over Java 9 ProcessHandle objects
Class<?> clazz = handle.getClass();
Stream<?> children = (Stream<?>) clazz.getMethod("descendants").invoke(handle);
clazz.getMethod("destroyForcibly").invoke(handle);
children.forEach(Executable::terminate9);
return false;
} catch (Throwable e) {
return true;
}
}
}

0 comments on commit 6890b94

Please sign in to comment.