36 lines
1.1 KiB
Java
36 lines
1.1 KiB
Java
|
package io.gitlab.jfronny.inceptum.util;
|
||
|
|
||
|
import io.gitlab.jfronny.inceptum.Inceptum;
|
||
|
|
||
|
import java.io.BufferedReader;
|
||
|
import java.io.InputStreamReader;
|
||
|
|
||
|
public class ProcessUtils {
|
||
|
public static boolean isProcessAlive(String pid) {
|
||
|
return isProcessIdRunning(pid, Utils.OS.equals("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;
|
||
|
}
|
||
|
}
|
||
|
}
|