2021-10-30 14:22:07 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.util;
|
|
|
|
|
|
|
|
import io.gitlab.jfronny.inceptum.Inceptum;
|
2021-10-31 16:59:25 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.model.OSType;
|
2021-10-30 14:22:07 +02:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
|
|
public class ProcessUtils {
|
|
|
|
public static boolean isProcessAlive(String pid) {
|
2021-10-31 16:59:25 +01:00
|
|
|
return isProcessIdRunning(pid, OSCheck.OS == OSType.WINDOWS
|
2021-10-30 14:22:07 +02:00
|
|
|
? "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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|