@Test public void testGetPidByProcessName_WithImproperData_ReturnsNothing() throws IOException, InterruptedException { InputStream stubInputStream = IOUtils.toInputStream( " PID TTY STAT TIME COMMAND\n 9990 ? Ss 0:07 /test/java\n " + this.oldProcessId + " ? Ss 0:07 /osc/java", Charset.defaultCharset()); PowerMockito.when(this.processMock.getInputStream()).thenReturn(stubInputStream); PowerMockito.when(this.runtimeMock.exec(this.linuxProcessExec)).thenReturn(this.processMock); Mockito.when(Runtime.getRuntime()).thenReturn(this.runtimeMock); String foundPid = ServerUtil.getPidByProcessName("/osc/test"); Assert.assertNull(foundPid); }
public static String getPidByProcessName(String processName) { try { String psArg = ""; if (isWindows()) { psArg += "-W"; } Process p = java.lang.Runtime.getRuntime().exec("ps " + psArg); int exitVal = p.waitFor(); log.info("ps process terminated with exit code " + exitVal); try(InputStream inputStream = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))){ String s; while ((s = reader.readLine()) != null) { s = s.trim(); log.debug(s); if (s.endsWith(processName)) { String pid = s.split(" +", -3)[0]; log.info("Found PID for " + processName + ": " + pid); return pid; } } } } catch (Exception e) { log.error("Fail to find process PID for '" + processName + "'", e); } return null; }
ServerUtil { public static String getPidByProcessName(String processName) { try { String psArg = ""; if (isWindows()) { psArg += "-W"; } Process p = java.lang.Runtime.getRuntime().exec("ps " + psArg); int exitVal = p.waitFor(); log.info("ps process terminated with exit code " + exitVal); try(InputStream inputStream = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))){ String s; while ((s = reader.readLine()) != null) { s = s.trim(); log.debug(s); if (s.endsWith(processName)) { String pid = s.split(" +", -3)[0]; log.info("Found PID for " + processName + ": " + pid); return pid; } } } } catch (Exception e) { log.error("Fail to find process PID for '" + processName + "'", e); } return null; } }
ServerUtil { public static String getPidByProcessName(String processName) { try { String psArg = ""; if (isWindows()) { psArg += "-W"; } Process p = java.lang.Runtime.getRuntime().exec("ps " + psArg); int exitVal = p.waitFor(); log.info("ps process terminated with exit code " + exitVal); try(InputStream inputStream = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))){ String s; while ((s = reader.readLine()) != null) { s = s.trim(); log.debug(s); if (s.endsWith(processName)) { String pid = s.split(" +", -3)[0]; log.info("Found PID for " + processName + ": " + pid); return pid; } } } } catch (Exception e) { log.error("Fail to find process PID for '" + processName + "'", e); } return null; }  }
ServerUtil { public static String getPidByProcessName(String processName) { try { String psArg = ""; if (isWindows()) { psArg += "-W"; } Process p = java.lang.Runtime.getRuntime().exec("ps " + psArg); int exitVal = p.waitFor(); log.info("ps process terminated with exit code " + exitVal); try(InputStream inputStream = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))){ String s; while ((s = reader.readLine()) != null) { s = s.trim(); log.debug(s); if (s.endsWith(processName)) { String pid = s.split(" +", -3)[0]; log.info("Found PID for " + processName + ": " + pid); return pid; } } } } catch (Exception e) { log.error("Fail to find process PID for '" + processName + "'", e); } return null; }  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 { public static String getPidByProcessName(String processName) { try { String psArg = ""; if (isWindows()) { psArg += "-W"; } Process p = java.lang.Runtime.getRuntime().exec("ps " + psArg); int exitVal = p.waitFor(); log.info("ps process terminated with exit code " + exitVal); try(InputStream inputStream = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))){ String s; while ((s = reader.readLine()) != null) { s = s.trim(); log.debug(s); if (s.endsWith(processName)) { String pid = s.split(" +", -3)[0]; log.info("Found PID for " + processName + ": " + pid); return pid; } } } } catch (Exception e) { log.error("Fail to find process PID for '" + processName + "'", e); } return null; }  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);  }