private static List<Long> getProcessIdsUnix() { String commands = "ps -e"; String message = commands; LOG.debug("Executing commands: " + message); List<Long> answer = new ArrayList<Long>(); try { Process process = Runtime.getRuntime().exec(commands); parseProcesses(process.getInputStream(), answer, message, Filters.<String>trueFilter(), null); processErrors(process.getErrorStream(), message); } catch (Exception e) { LOG.error("Failed to execute process " + "stdin" + " for " + message + ": " + e, e); } return answer; }
protected static int killProcessUnix(Long pid, String params) { String commands = "kill " + (params != null ? params + " " : "") + pid; Process process = null; Runtime runtime = Runtime.getRuntime(); LOG.debug("Executing commands: " + commands); try { process = runtime.exec(commands); processInput(process.getInputStream(), commands); processErrors(process.getErrorStream(), commands); } catch (Exception e) { LOG.error("Failed to execute process " + "stdin" + " for " + commands + ": " + e, e); } try { return process != null ? process.waitFor() : 1; } catch (InterruptedException e) { String message = format("Interrupted while waiting for 'kill %d ' command to finish", pid); throw new RuntimeException(message, e); } }
protected static int killProcessWindows(Long pid, String params) { String commands = "taskkill " + (params != null ? params + " " : "") + "/PID " + pid; Process process = null; Runtime runtime = Runtime.getRuntime(); LOG.debug("Executing commands: " + commands); try { process = runtime.exec(commands); processInput(process.getInputStream(), commands); processErrors(process.getErrorStream(), commands); } catch (Exception e) { LOG.error("Failed to execute process " + "stdin" + " for " + commands + ": " + e, e); } try { return process != null ? process.waitFor() : 1; } catch (InterruptedException e) { String message = format("Interrupted while waiting for 'taskkill /PID %d ' command to finish", pid); throw new RuntimeException(message, e); } }
/** * Returns a list of active docker containers */ public static int killDockerContainer(String containerId) { // lets log to the console too as this tends to show up in the junit output System.out.println("Killing Docker container " + containerId); LOG.warn("WARNING: Killing Docker container " + containerId); String commands = "docker kill " + containerId; String message =commands; LOG.debug("Executing commands: " + message); final List<String> answer = new ArrayList<>(); Process process = null; try { process = Runtime.getRuntime().exec(commands); processInput(process.getInputStream(), commands); processErrors(process.getErrorStream(), commands); } catch (Exception e) { LOG.error("Failed to execute process " + "stdin" + " for " + message + ": " + e, e); } return process != null ? process.exitValue() : -1; }
/** * Returns a list of active docker containers */ public static List<String> getDockerContainerIds() { String commands = "docker ps -q"; String message = "output of command: " + commands; LOG.debug("Executing commands: " + message); final List<String> answer = new ArrayList<>(); try { Process process = Runtime.getRuntime().exec(commands); Function<String, Void> fn = new Function<String, Void>() { @Override public Void apply(String line) { if (Strings.isNotBlank(line)) { answer.add(line.trim()); } return null; } }; processOutput(process.getInputStream(), fn, message); processErrors(process.getErrorStream(), message); } catch (Exception e) { LOG.error("Failed to execute process " + "stdin" + " for " + message + ": " + e, e); } return answer; }
/** * Returns the list of current active PIDs for any java based process * that has a main class which contains any of the given bits of text */ public static List<Long> getJavaProcessIds(String... classNameFilter) { String commands = "jps -l"; String message = commands; LOG.debug("Executing commands: " + message); List<Long> answer = new ArrayList<Long>(); Filter<String> filter = Filters.containsAnyString(classNameFilter); try { Process process = Runtime.getRuntime().exec(commands); parseProcesses(process.getInputStream(), answer, message, filter, null); processErrors(process.getErrorStream(), message); } catch (Exception e) { LOG.error("Failed to execute process " + "stdin" + " for " + message + ": " + e, e); } return answer; }
private static List<Long> getProcessIdsWindows() { String commands = "tasklist /NH"; String message = commands; LOG.debug("Executing commands: " + message); List<Long> answer = new ArrayList<Long>(); try { Process process = Runtime.getRuntime().exec(commands); // on windows the tasklist returns memory usage as numbers, so we need to chop the line so we only include // the pid numbers parseProcesses(process.getInputStream(), answer, message, Filters.<String>trueFilter(), Functions.chopLength(50)); processErrors(process.getErrorStream(), message); } catch (Exception e) { LOG.error("Failed to execute process " + "stdin" + " for " + message + ": " + e, e); } return answer; }