mirror of
https://github.com/ME1312/SubServers-2.git
synced 2024-11-22 02:08:27 +01:00
Implement Java 9 process termination
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?
This commit is contained in:
parent
52fd155b5f
commit
6890b94b16
@ -6,6 +6,7 @@ import net.ME1312.SubServers.Bungee.Library.Compatibility.JNA;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Executable Handler Class
|
||||
@ -93,18 +94,28 @@ public class Executable {
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,6 @@ import java.util.function.BiConsumer;
|
||||
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;
|
||||
|
@ -6,6 +6,7 @@ import net.ME1312.SubServers.Host.Library.Compatibility.JNA;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Executable Handler Class
|
||||
@ -93,18 +94,28 @@ public class Executable {
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user