@Test @SuppressWarnings("unchecked") public void testKillProcess_WithLongRunningProcessId_UsesForcedShutdown() throws Exception { final boolean[] testPassed = new boolean[1]; final List<String> gentleShutdownIterationsCounter = new ArrayList<>(); replace(method(ServerUtil.class, "execWithLines", String.class, List.class)).with( new InvocationHandler() { @Override public Object invoke(Object object, Method method, Object[] arguments) throws Throwable { if (arguments[0].equals("ps " + ServerUtilTest.this.oldProcessId)) { List<String> test = (List<String>) arguments[1]; test.addAll(ServerUtilTest.this.cmdOutput); gentleShutdownIterationsCounter.add("invocation"); return 0; } else { return method.invoke(object, arguments); } } }); replaceExecWithLogMethod(this.oldProcessId, testPassed); ServerUtil.killProcess(this.oldProcessId); Assert.assertTrue(gentleShutdownIterationsCounter.size() == 10); Assert.assertTrue(testPassed[0]); }
static void killProcess(String oldPid) throws InterruptedException { int retries = 10; boolean pidFound = false; while (retries > 0) { List<String> lines = new ArrayList<>(); ServerUtil.execWithLines("ps " + oldPid, lines); pidFound = false; for (String line : lines) { if (line.contains(oldPid)) { pidFound = true; break; } } if (!pidFound) { break; } log.info("Old process (" + oldPid + ") is still running. Retry (" + retries + ") wait for graceful termination."); retries--; Thread.sleep(500); } if (pidFound) { log.warn("Old process (" + oldPid + ") is still running. Triggering forceful termination."); ServerUtil.execWithLog("kill -9 " + oldPid); } }
ServerUtil { static void killProcess(String oldPid) throws InterruptedException { int retries = 10; boolean pidFound = false; while (retries > 0) { List<String> lines = new ArrayList<>(); ServerUtil.execWithLines("ps " + oldPid, lines); pidFound = false; for (String line : lines) { if (line.contains(oldPid)) { pidFound = true; break; } } if (!pidFound) { break; } log.info("Old process (" + oldPid + ") is still running. Retry (" + retries + ") wait for graceful termination."); retries--; Thread.sleep(500); } if (pidFound) { log.warn("Old process (" + oldPid + ") is still running. Triggering forceful termination."); ServerUtil.execWithLog("kill -9 " + oldPid); } } }
ServerUtil { static void killProcess(String oldPid) throws InterruptedException { int retries = 10; boolean pidFound = false; while (retries > 0) { List<String> lines = new ArrayList<>(); ServerUtil.execWithLines("ps " + oldPid, lines); pidFound = false; for (String line : lines) { if (line.contains(oldPid)) { pidFound = true; break; } } if (!pidFound) { break; } log.info("Old process (" + oldPid + ") is still running. Retry (" + retries + ") wait for graceful termination."); retries--; Thread.sleep(500); } if (pidFound) { log.warn("Old process (" + oldPid + ") is still running. Triggering forceful termination."); ServerUtil.execWithLog("kill -9 " + oldPid); } }  }
ServerUtil { static void killProcess(String oldPid) throws InterruptedException { int retries = 10; boolean pidFound = false; while (retries > 0) { List<String> lines = new ArrayList<>(); ServerUtil.execWithLines("ps " + oldPid, lines); pidFound = false; for (String line : lines) { if (line.contains(oldPid)) { pidFound = true; break; } } if (!pidFound) { break; } log.info("Old process (" + oldPid + ") is still running. Retry (" + retries + ") wait for graceful termination."); retries--; Thread.sleep(500); } if (pidFound) { log.warn("Old process (" + oldPid + ") is still running. Triggering forceful termination."); ServerUtil.execWithLog("kill -9 " + oldPid); } }  static long getUptime(); static String uptimeToString(); static String uptimeToString(long uptime); static Long getUsableDiscSpaceInGB(); static boolean isEnoughSpace(); static String getCurrentPid(); static boolean isWindows(); static void stopServerProcess(String pidFile); static void terminateProcessByPid(Integer pid); static void writePIDToFile(String filename); static void writePIDToFile(String filename, String pid); static String getPidByProcessName(String processName); static void deletePidFileIfOwned(String filename); static boolean startAgentProcess(); static boolean startAgentProcess(int waitTimeout, ServerServiceChecker serverServiceChecker); static boolean startServerProcess(); static boolean startServerProcess(int waitTimeout, ServerServiceChecker serverServiceChecker); static boolean startServerProcess(int waitTimeout, ServerServiceChecker serverServiceChecker,
            boolean isRebootAfterFailure); static int execWithLog(String cmd); static int execWithLog(String[] cmd); static int execWithLog(String cmd, List<String> lines); static int execWithLog(String[] cmd, List<String> lines); static int execWithLog(String[] cmd, boolean logCommandLine); static int execWithLog(String[] cmd, List<String> lines, boolean logCommandLine); static int execWithLines(String cmd, List<String> outLines); static int execWithLines(String[] cmd, List<String> outLines, boolean logCommand); static String getServerIP(); static void setServerIP(String serverIP); static Thread getTimeMonitorThread(final TimeChangeCommand timeChangeCommand, final long timeChangeThreshold,
            final long sleepInterval); }
ServerUtil { static void killProcess(String oldPid) throws InterruptedException { int retries = 10; boolean pidFound = false; while (retries > 0) { List<String> lines = new ArrayList<>(); ServerUtil.execWithLines("ps " + oldPid, lines); pidFound = false; for (String line : lines) { if (line.contains(oldPid)) { pidFound = true; break; } } if (!pidFound) { break; } log.info("Old process (" + oldPid + ") is still running. Retry (" + retries + ") wait for graceful termination."); retries--; Thread.sleep(500); } if (pidFound) { log.warn("Old process (" + oldPid + ") is still running. Triggering forceful termination."); ServerUtil.execWithLog("kill -9 " + oldPid); } }  static long getUptime(); static String uptimeToString(); static String uptimeToString(long uptime); static Long getUsableDiscSpaceInGB(); static boolean isEnoughSpace(); static String getCurrentPid(); static boolean isWindows(); static void stopServerProcess(String pidFile); static void terminateProcessByPid(Integer pid); static void writePIDToFile(String filename); static void writePIDToFile(String filename, String pid); static String getPidByProcessName(String processName); static void deletePidFileIfOwned(String filename); static boolean startAgentProcess(); static boolean startAgentProcess(int waitTimeout, ServerServiceChecker serverServiceChecker); static boolean startServerProcess(); static boolean startServerProcess(int waitTimeout, ServerServiceChecker serverServiceChecker); static boolean startServerProcess(int waitTimeout, ServerServiceChecker serverServiceChecker,
            boolean isRebootAfterFailure); static int execWithLog(String cmd); static int execWithLog(String[] cmd); static int execWithLog(String cmd, List<String> lines); static int execWithLog(String[] cmd, List<String> lines); static int execWithLog(String[] cmd, boolean logCommandLine); static int execWithLog(String[] cmd, List<String> lines, boolean logCommandLine); static int execWithLines(String cmd, List<String> outLines); static int execWithLines(String[] cmd, List<String> outLines, boolean logCommand); static String getServerIP(); static void setServerIP(String serverIP); static Thread getTimeMonitorThread(final TimeChangeCommand timeChangeCommand, final long timeChangeThreshold,
            final long sleepInterval);  }