@Test public void processTimeout() { String[] cmdarray = {"sleep", "120"}; ProcessExecutor executor = new ProcessExecutor.Builder(cmdarray).setWait(1).build(); assertEquals(false, executor.execute()); }
public boolean execute() { boolean completed = false; Process process = null; try { process = Runtime.getRuntime().exec(cmdarray); if (stdin != null) { OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream()); osw.write(stdin); osw.close(); } completed = process.waitFor(wait, TimeUnit.SECONDS); if (!completed) { stdout = readAvailable(process.getInputStream()); stderr = readAvailable(process.getErrorStream()); process.destroyForcibly(); } else { stdout = new String(ByteStreams.toByteArray(process.getInputStream())); stderr = new String(ByteStreams.toByteArray(process.getErrorStream())); exitValue = process.exitValue(); } if (!completed) { log.error("Process '{}' was killed. It did not complete after {} seconds. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), wait, stderr, stderr); } else if (exitValue != 0) { log.error("Process '{}' returned non-zero exit value {}. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), exitValue, stderr, stderr); } } catch (IOException|InterruptedException ex) { log.error("Exception occurred attempting to execute process '{}'", Arrays.toString(cmdarray), ex); if (process != null) { process.destroyForcibly(); } } return completed; }
ProcessExecutor { public boolean execute() { boolean completed = false; Process process = null; try { process = Runtime.getRuntime().exec(cmdarray); if (stdin != null) { OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream()); osw.write(stdin); osw.close(); } completed = process.waitFor(wait, TimeUnit.SECONDS); if (!completed) { stdout = readAvailable(process.getInputStream()); stderr = readAvailable(process.getErrorStream()); process.destroyForcibly(); } else { stdout = new String(ByteStreams.toByteArray(process.getInputStream())); stderr = new String(ByteStreams.toByteArray(process.getErrorStream())); exitValue = process.exitValue(); } if (!completed) { log.error("Process '{}' was killed. It did not complete after {} seconds. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), wait, stderr, stderr); } else if (exitValue != 0) { log.error("Process '{}' returned non-zero exit value {}. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), exitValue, stderr, stderr); } } catch (IOException|InterruptedException ex) { log.error("Exception occurred attempting to execute process '{}'", Arrays.toString(cmdarray), ex); if (process != null) { process.destroyForcibly(); } } return completed; } }
ProcessExecutor { public boolean execute() { boolean completed = false; Process process = null; try { process = Runtime.getRuntime().exec(cmdarray); if (stdin != null) { OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream()); osw.write(stdin); osw.close(); } completed = process.waitFor(wait, TimeUnit.SECONDS); if (!completed) { stdout = readAvailable(process.getInputStream()); stderr = readAvailable(process.getErrorStream()); process.destroyForcibly(); } else { stdout = new String(ByteStreams.toByteArray(process.getInputStream())); stderr = new String(ByteStreams.toByteArray(process.getErrorStream())); exitValue = process.exitValue(); } if (!completed) { log.error("Process '{}' was killed. It did not complete after {} seconds. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), wait, stderr, stderr); } else if (exitValue != 0) { log.error("Process '{}' returned non-zero exit value {}. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), exitValue, stderr, stderr); } } catch (IOException|InterruptedException ex) { log.error("Exception occurred attempting to execute process '{}'", Arrays.toString(cmdarray), ex); if (process != null) { process.destroyForcibly(); } } return completed; } private  ProcessExecutor(@Nonnull String[] cmdarray, @Nullable String stdin,
                           @Time(TimeUnit.SECONDS) int wait); }
ProcessExecutor { public boolean execute() { boolean completed = false; Process process = null; try { process = Runtime.getRuntime().exec(cmdarray); if (stdin != null) { OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream()); osw.write(stdin); osw.close(); } completed = process.waitFor(wait, TimeUnit.SECONDS); if (!completed) { stdout = readAvailable(process.getInputStream()); stderr = readAvailable(process.getErrorStream()); process.destroyForcibly(); } else { stdout = new String(ByteStreams.toByteArray(process.getInputStream())); stderr = new String(ByteStreams.toByteArray(process.getErrorStream())); exitValue = process.exitValue(); } if (!completed) { log.error("Process '{}' was killed. It did not complete after {} seconds. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), wait, stderr, stderr); } else if (exitValue != 0) { log.error("Process '{}' returned non-zero exit value {}. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), exitValue, stderr, stderr); } } catch (IOException|InterruptedException ex) { log.error("Exception occurred attempting to execute process '{}'", Arrays.toString(cmdarray), ex); if (process != null) { process.destroyForcibly(); } } return completed; } private  ProcessExecutor(@Nonnull String[] cmdarray, @Nullable String stdin,
                           @Time(TimeUnit.SECONDS) int wait); boolean execute(); int exitValue(); String stdout(); String stderr(); }
ProcessExecutor { public boolean execute() { boolean completed = false; Process process = null; try { process = Runtime.getRuntime().exec(cmdarray); if (stdin != null) { OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream()); osw.write(stdin); osw.close(); } completed = process.waitFor(wait, TimeUnit.SECONDS); if (!completed) { stdout = readAvailable(process.getInputStream()); stderr = readAvailable(process.getErrorStream()); process.destroyForcibly(); } else { stdout = new String(ByteStreams.toByteArray(process.getInputStream())); stderr = new String(ByteStreams.toByteArray(process.getErrorStream())); exitValue = process.exitValue(); } if (!completed) { log.error("Process '{}' was killed. It did not complete after {} seconds. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), wait, stderr, stderr); } else if (exitValue != 0) { log.error("Process '{}' returned non-zero exit value {}. " + "Stdout was '{}' and stderr was '{}'", Arrays.toString(cmdarray), exitValue, stderr, stderr); } } catch (IOException|InterruptedException ex) { log.error("Exception occurred attempting to execute process '{}'", Arrays.toString(cmdarray), ex); if (process != null) { process.destroyForcibly(); } } return completed; } private  ProcessExecutor(@Nonnull String[] cmdarray, @Nullable String stdin,
                           @Time(TimeUnit.SECONDS) int wait); boolean execute(); int exitValue(); String stdout(); String stderr();  }