/** * Attempts to kill the given process */ public static int killProcess(Long pid, String params) { if (pid == null || !isProcessAlive(pid)) { return 0; } if (isWindows) { if ("-9".equals(params)) { params = "/F"; } return killProcessWindows(pid, params); } else { return killProcessUnix(pid, params); } }
/** * Kills all java processes found which include the classNameFilter in their main class */ public static int killJavaProcesses(String... classNameFilters) { int count = 0; List<Long> javaProcessIds = getJavaProcessIds(classNameFilters); for (Long processId : javaProcessIds) { // lets log to the console too as this tends to show up in the junit output System.out.println("WARNING: Killing Java process " + processId); LOG.warn("Killing Java process " + processId); killProcess(processId, "-9"); count++; } return count; }
@Override public void run() { int count = 0; List<String> ids = getDockerContainerIds(); for (String id : ids) { if (killDockerContainer(id) == 0) { count++; } } } }).run();
/** * Returns the list of current active PIDs */ public static List<Long> getProcessIds() { if (isWindows) { return getProcessIdsWindows(); } else { return getProcessIdsUnix(); } }
@Override public int kill() throws Exception { String customCommand = config.getKillCommand(); if (Strings.isNullOrEmpty(customCommand)) { // lets stop it LOG.info("No kill command configured so lets just try killing it with -9 " + this); return Processes.killProcess(getPid(), "-9"); } return runConfigCommandValueOrLaunchScriptWith(customCommand, "kill"); }
/** * Returns the PID for the process; checking first if its still active. * * @return null if the process is no longer active */ public Long getActivePid() throws IOException { ProcessController aController = getController(); Long answer = null; if (aController != null) { answer = aController.getPid(); } if (answer != null) { if (!Processes.isProcessAlive(answer)) { answer = null; } } return answer; } }
/** * Kills all commonly created Java based processes created by fabric8 and its unit tests. * * This is handy for unit testing to ensure there's no stray karaf, wildfly, tomcat or java containers running. */ public static int killJavaProcesses() { return killJavaProcesses("karaf", "jboss", "catalina", "spring.Main", "FabricSpringApplication"); }
/** * Returns true if the given PID is still alive */ public static boolean isProcessAlive(long pid) { List<Long> processIds = getProcessIds(); if (processIds.isEmpty()) { // we must be on a platform that the PID list doesn't work like windows return true; } return processIds.contains(pid); }
@Override public int stop() throws Exception { String customCommand = config.getStopCommand(); if (Strings.isNullOrEmpty(customCommand)) { // lets just kill it LOG.info("No stop command configured so lets just try killing it " + this); return Processes.killProcess(getPid(), ""); } return runConfigCommandValueOrLaunchScriptWith(customCommand, "stop"); }