Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/util/ProcessUtils.java

37 lines
1.2 KiB
Java

package io.gitlab.jfronny.inceptum.launcher.util;
import io.gitlab.jfronny.commons.OSUtils;
import io.gitlab.jfronny.inceptum.common.Utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcessUtils {
public static boolean isProcessAlive(String pid) {
if (OSUtils.TYPE == OSUtils.Type.WINDOWS)
return isProcessIdRunning(pid, "tasklist", "/FI", "PID eq " + pid);
return isProcessIdRunning(pid, "ps", "-p", pid);
}
private static boolean isProcessIdRunning(String pid, String... command) {
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
try (InputStreamReader isReader = new InputStreamReader(pr.inputStream);
BufferedReader bReader = new BufferedReader(isReader)) {
String strLine;
while ((strLine = bReader.readLine()) != null) {
if (strLine.contains(" " + pid + " ")) {
return true;
}
}
}
return false;
} catch (Exception e) {
Utils.LOGGER.error("Could not get process state", e);
return true;
}
}
}