public String runCommand(String com) { StringBuffer output = new StringBuffer(); Process p; String line = ""; try { p = Runtime.getRuntime().exec(com); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((reader.readLine()) != null) { output.append(line = reader.readLine() + "\n"); } } catch (Exception e) { e.printStackTrace(); } return output.toString(); }
/** * Checks if address can be reached using one argument InetAddress.isReachable() version or ping * command if failed. * * @param addr Address to check. * @param reachTimeout Timeout for the check. * @return {@code True} if address is reachable. */ public static boolean reachableByPing(InetAddress addr, int reachTimeout) { try { if (addr.isReachable(reachTimeout)) return true; String cmd = String.format("ping -%s 1 %s", U.isWindows() ? "n" : "c", addr.getHostAddress()); Process myProc = Runtime.getRuntime().exec(cmd); myProc.waitFor(); return myProc.exitValue() == 0; } catch (IOException ignore) { return false; } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); return false; } }
private void diagnoseFailedLoad(Exception cause) { Process proc = process.getProcess(); try { int val = proc.exitValue(); new RuntimeException("Jenkins died loading. Exit code " + val, cause); } catch (IllegalThreadStateException _) { // Process alive } // Try to get stacktrace Class<?> clazz; Field pidField; try { clazz = Class.forName("java.lang.UNIXProcess"); pidField = clazz.getDeclaredField("pid"); pidField.setAccessible(true); } catch (Exception e) { LinkageError x = new LinkageError(); x.initCause(e); throw x; } if (clazz.isAssignableFrom(proc.getClass())) { int pid; try { pid = (int) pidField.get(proc); } catch (IllegalArgumentException | IllegalAccessException e) { throw new AssertionError(e); } try { Process jstack = new ProcessBuilder("jstack", String.valueOf(pid)).start(); if (jstack.waitFor() == 0) { StringWriter writer = new StringWriter(); IOUtils.copy(jstack.getInputStream(), writer); RuntimeException ex = new RuntimeException( cause.getMessage() + "\n\n" + writer.toString() ); ex.setStackTrace(cause.getStackTrace()); throw ex; } } catch (IOException | InterruptedException e) { throw new AssertionError(e); } } throw new Error(cause); }