@Test public void execCommandTolerateFailureSucceed() throws Exception { File testDir = AlluxioTestDirectory.createTemporaryDirectory("command"); File testFile = createFileInDir(testDir, "testFile"); String[] testCommandSucceed = new String[]{"ls", String.format("%s", testDir.getAbsolutePath())}; CommandReturn crs = new ShellCommand(testCommandSucceed).runWithOutput(); assertEquals(0, crs.getExitCode()); assertTrue(crs.getOutput().contains(testFile.getName())); }
public CommandReturn runWithOutput() throws IOException { Process process = null; BufferedReader inReader = null; try { process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder stdout = new StringBuilder(); String outLine = inReader.readLine(); while (outLine != null) { stdout.append(outLine); stdout.append("\n"); outLine = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { LOG.warn(String.format("Non-zero exit code (%d) from command %s", exitCode, Arrays.toString(mCommand))); } CommandReturn cr = new CommandReturn(exitCode, mCommand, stdout.toString()); if (process != null) { process.destroy(); } return cr; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } catch (Exception e) { return new CommandReturn(1, String.format("Command %s failed, exception is %s", Arrays.toString(mCommand), e.getMessage())); } finally { if (inReader != null) { inReader.close(); } if (process != null) { process.destroy(); } } }
ShellCommand { public CommandReturn runWithOutput() throws IOException { Process process = null; BufferedReader inReader = null; try { process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder stdout = new StringBuilder(); String outLine = inReader.readLine(); while (outLine != null) { stdout.append(outLine); stdout.append("\n"); outLine = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { LOG.warn(String.format("Non-zero exit code (%d) from command %s", exitCode, Arrays.toString(mCommand))); } CommandReturn cr = new CommandReturn(exitCode, mCommand, stdout.toString()); if (process != null) { process.destroy(); } return cr; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } catch (Exception e) { return new CommandReturn(1, String.format("Command %s failed, exception is %s", Arrays.toString(mCommand), e.getMessage())); } finally { if (inReader != null) { inReader.close(); } if (process != null) { process.destroy(); } } } }
ShellCommand { public CommandReturn runWithOutput() throws IOException { Process process = null; BufferedReader inReader = null; try { process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder stdout = new StringBuilder(); String outLine = inReader.readLine(); while (outLine != null) { stdout.append(outLine); stdout.append("\n"); outLine = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { LOG.warn(String.format("Non-zero exit code (%d) from command %s", exitCode, Arrays.toString(mCommand))); } CommandReturn cr = new CommandReturn(exitCode, mCommand, stdout.toString()); if (process != null) { process.destroy(); } return cr; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } catch (Exception e) { return new CommandReturn(1, String.format("Command %s failed, exception is %s", Arrays.toString(mCommand), e.getMessage())); } finally { if (inReader != null) { inReader.close(); } if (process != null) { process.destroy(); } } } ShellCommand(String[] execString); }
ShellCommand { public CommandReturn runWithOutput() throws IOException { Process process = null; BufferedReader inReader = null; try { process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder stdout = new StringBuilder(); String outLine = inReader.readLine(); while (outLine != null) { stdout.append(outLine); stdout.append("\n"); outLine = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { LOG.warn(String.format("Non-zero exit code (%d) from command %s", exitCode, Arrays.toString(mCommand))); } CommandReturn cr = new CommandReturn(exitCode, mCommand, stdout.toString()); if (process != null) { process.destroy(); } return cr; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } catch (Exception e) { return new CommandReturn(1, String.format("Command %s failed, exception is %s", Arrays.toString(mCommand), e.getMessage())); } finally { if (inReader != null) { inReader.close(); } if (process != null) { process.destroy(); } } } ShellCommand(String[] execString); String run(); CommandReturn runWithOutput(); String toString(); String[] getCommand(); }
ShellCommand { public CommandReturn runWithOutput() throws IOException { Process process = null; BufferedReader inReader = null; try { process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder stdout = new StringBuilder(); String outLine = inReader.readLine(); while (outLine != null) { stdout.append(outLine); stdout.append("\n"); outLine = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { LOG.warn(String.format("Non-zero exit code (%d) from command %s", exitCode, Arrays.toString(mCommand))); } CommandReturn cr = new CommandReturn(exitCode, mCommand, stdout.toString()); if (process != null) { process.destroy(); } return cr; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } catch (Exception e) { return new CommandReturn(1, String.format("Command %s failed, exception is %s", Arrays.toString(mCommand), e.getMessage())); } finally { if (inReader != null) { inReader.close(); } if (process != null) { process.destroy(); } } } ShellCommand(String[] execString); String run(); CommandReturn runWithOutput(); String toString(); String[] getCommand();  }