2022-09-04 21:21:24 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.launcher.util;
|
2021-10-30 14:22:07 +02:00
|
|
|
|
2022-04-28 23:13:37 +02:00
|
|
|
import io.gitlab.jfronny.commons.OSUtils;
|
2022-09-04 21:21:24 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.common.Utils;
|
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) {
|
2022-12-17 21:53:21 +01:00
|
|
|
if (OSUtils.TYPE == OSUtils.Type.WINDOWS)
|
|
|
|
return isProcessIdRunning(pid, "tasklist", "/FI", "PID eq " + pid);
|
|
|
|
return isProcessIdRunning(pid, "ps", "-p", pid);
|
2021-10-30 14:22:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-17 21:53:21 +01:00
|
|
|
private static boolean isProcessIdRunning(String pid, String... command) {
|
2021-10-30 14:22:07 +02:00
|
|
|
try {
|
|
|
|
Runtime rt = Runtime.getRuntime();
|
|
|
|
Process pr = rt.exec(command);
|
|
|
|
|
2022-11-18 21:58:40 +01:00
|
|
|
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;
|
|
|
|
}
|
2021-10-30 14:22:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} catch (Exception e) {
|
2022-01-18 18:43:39 +01:00
|
|
|
Utils.LOGGER.error("Could not get process state", e);
|
2021-10-30 14:22:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|