Programming Language/Java
running process 확인 로직
하루y
2024. 11. 11. 22:23
jsp 를 이용한 로직, 또는 windows 의 tasklist 나, unix/linux의 ps 를 이용할 수도 있다.
private static boolean checkRunningJarProcesses(String checkProcess) {
boolean jarProcessFound = false;
if(checkProcess == null) {
return jarProcessFound;
}
checkProcess = checkProcess.trim();
// 환경설정. 1) JAVA_HOME 정의하고, 2) path에 %JAVA_HOME%\bin 추가할 것.
String command = "jps";
String line;
Process process = null;
try {
process = new ProcessBuilder(command).start();
logger.debug("Checking for running JAR processes...");
try(BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
while ((line = reader.readLine()) != null) {
// "java -jar"가 포함된 프로세스를 찾음
String[] columns = line.split(" "); // 1: pid, 2: process
if (columns.length > 1 && checkProcess.equalsIgnoreCase(columns[1])) {
logger.debug("Found JAR process: " + line);
jarProcessFound = true;
}
}
}
// int returnCode = process.waitFor();
// logger.debug("returnCode: {}", returnCode);
} catch (IOException e) {
logger.error(e.toString());
} finally {
if(process != null) {
process.destroy();
}
}
return jarProcessFound;
}
end.
728x90