Inceptum/src/main/java/io/gitlab/jfronny/inceptum/util/ProcessUtils.java
2021-10-31 16:59:25 +01:00

37 lines
1.1 KiB
Java

package io.gitlab.jfronny.inceptum.util;
import io.gitlab.jfronny.inceptum.Inceptum;
import io.gitlab.jfronny.inceptum.model.OSType;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcessUtils {
public static boolean isProcessAlive(String pid) {
return isProcessIdRunning(pid, OSCheck.OS == OSType.WINDOWS
? "cmd /c tasklist /FI \"PID eq " + pid + "\""
: "ps -p " + pid);
}
private static boolean isProcessIdRunning(String pid, String command) {
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
InputStreamReader isReader = new InputStreamReader(pr.getInputStream());
BufferedReader bReader = new BufferedReader(isReader);
String strLine;
while ((strLine= bReader.readLine()) != null) {
if (strLine.contains(" " + pid + " ")) {
return true;
}
}
return false;
} catch (Exception e) {
Inceptum.LOGGER.error("Could not get process state", e);
return true;
}
}
}