cmdLine = new CommandLine(cmdArray[0]); cmdLine.addArgument(cmdArray[i], false); cmdLine = CommandLine.parse(commandLine); Executor executor = new DefaultExecutor(); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(stdout); executor.setExitValue(1); executor.setStreamHandler(streamHandler); executor.setWatchdog(watchdog); executor.execute(cmdLine, resultHandler); logger.debug("executed commandLine '{}'", commandLine); } catch (IOException e) { resultHandler.waitFor(); int exitCode = resultHandler.getExitValue(); retval = StringUtils.chomp(stdout.toString()); if (resultHandler.getException() != null) { logger.warn("{}", resultHandler.getException().getMessage());
private int execute(final Logger logger, final OutputStream stdout, final OutputStream stderr) throws ProcessExecutionException { logger.debug("Executing command line {}", commandLine); try { ExecuteStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr); executor.setStreamHandler(streamHandler); int exitValue = executor.execute(commandLine, environment); logger.debug("Exit value {}", exitValue); return exitValue; } catch (ExecuteException e) { if (executor.getWatchdog() != null && executor.getWatchdog().killedProcess()) { throw new ProcessExecutionException("Process killed after timeout"); } throw new ProcessExecutionException(e); } catch (IOException e) { throw new ProcessExecutionException(e); } }
protected String executeExternalJavaProcess(Class<?> mainClass) throws IOException { String classpath = MvelTestUtils.getClassPath(); //See javadoc on MvelOverloadFailureReproduction for description of why we need to execute the test in a new JVM CommandLine cmdLine = new CommandLine("java"); cmdLine.addArgument("-cp"); cmdLine.addArgument(classpath, true); cmdLine.addArgument(mainClass.getName()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(baos)); try { executor.execute(cmdLine, new HashMap<String, String>()); } catch (IOException e) { throw new IOException(new String(baos.toByteArray())); } return new String(baos.toByteArray()); } }
/** * @VisibleForTesting */ void doProcess(final InputStream in, final OutputStream out) throws ExecuteException, IOException { final ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); final Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(out, errorStream, in)); final int result = executor.execute(CommandLine.parse(NGMIN_COMMAND)); LOG.debug("result={}", result); if (result != 0) { throw new ExecuteException("Processing failed: " + new String(errorStream.toByteArray()), result); } }
CommandLine cmdLine = new CommandLine("yarn"); cmdLine.addArgument("application"); cmdLine.addArgument("-list"); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); Executor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setStreamHandler(psh); try { executor.execute(cmdLine); } catch (IOException e) { logger.error(e.toString()); YarnApplicationWrapper wrapper = new YarnApplicationWrapper(stdout.toString()); YarnApplication app = wrapper.getApplication(job.getName()); if (app != null) { killCmdLine.addArgument("-kill"); killCmdLine.addArgument(app.getId()); Executor killExecutor = new DefaultExecutor(); killExecutor.setExitValue(0); try { killExecutor.execute(killCmdLine); } catch (IOException e) { logger.error(e.toString());
public void execute(CommandLine cmd, @Nullable File workingDirectory) throws InterruptedException, IOException { LOG.info("executing command {}", cmd.toString()); final ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000); final Executor executor = new org.apache.commons.exec.DefaultExecutor(); executor.setWatchdog(watchdog); if (workingDirectory != null) { executor.setWorkingDirectory(workingDirectory); } LOG.debug("workdir: {}", executor.getWorkingDirectory()); final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ByteArrayOutputStream err = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(out, err)); executor.execute(cmd, resultHandler); resultHandler.waitFor(); final int exitCode = resultHandler.getExitValue(); if (out.size() > 0) { LOG.info(out.toString()); } if (exitCode != 0) { throw new IOException(err.size() == 0 ? "command '" + cmd + "' didn't succeed" : err.toString()); } } }
CommandLine commandLine = new CommandLine(execAndArgs[0]); commandLine.addArguments(Arrays.copyOfRange(execAndArgs, 1, execAndArgs.length)); Executor executor = new DefaultExecutor(); executor.setExitValue(0); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baes = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream((stdInLine + "\n").getBytes()); PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(baos, baes, bais); executor.setStreamHandler(pumpStreamHandler); executor.setWatchdog(watchdog); executor.execute(commandLine); } catch (IOException e) { log.error("Script exited with non-zero exit code"); log.error("Stdout = "); log.error(baos.toString()); log.error("Stderr = "); log.error(baes.toString());
try (ByteArrayOutputStream localStdoutStream = new ByteArrayOutputStream(); ByteArrayOutputStream localStdErrStream = new ByteArrayOutputStream()) { OutputStream stdout = localStdoutStream; OutputStream stderr = localStdErrStream; String utf8 = StandardCharsets.UTF_8.name(); try { final CommandLine parse = CommandLine.parse(command); Executor executor = new DefaultExecutor(); if (workingDir != null) { LOG.info("working directory is " + workingDir.toString()); executor.setWorkingDirectory(workingDir); executor.setExitValue(0); if (dumpOutput) { LOG.info("CMD: " + command); executor.setStreamHandler(new PumpStreamHandler(stdout, stderr)); executor.execute(parse, resultHandler); resultHandler.waitFor(); throw new ExecuteException("problems running command: " + command, resultHandler.getExitValue()); return new ImmutablePair<>(localStdoutStream.toString(utf8), localStdErrStream.toString(utf8)); } catch (InterruptedException | IOException e) { throw new RuntimeException("problems running command: " + command, e);
System.out.println(commandline.toString()); Map<String, String> env = null; Map<String, String> entry = new HashMap<String, String>(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 10000); Executor executor = new DefaultExecutor(); executor.setExitValue(1); executor.setWatchdog(watchdog); executor.setStreamHandler(streamHandler); try { executor.execute(commandline, env, resultHandler); } catch (ExecuteException e) { LOG.debug("outputStream: " + outputStream.toString()); entry.put("exitValue", String.valueOf(resultHandler.getExitValue())); entry.put("outputStream", outputStream.toString() + e.getMessage()); e.printStackTrace(); return entry;
CommandLine cmdLine = new CommandLine("bin/logisland-launch-spark-job"); cmdLine.addArgument("--agent"); cmdLine.addArgument("http://0.0.0.0:8081"); cmdLine.addArgument("--job"); cmdLine.addArgument(jobId); executor.setWatchdog(watchdog); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); executor.setExitValue(0); executor.setStreamHandler(psh); try { executor.execute(cmdLine, resultHandler); } catch (IOException e) { e.printStackTrace();
final Executor e = new DefaultExecutor(); if (this.workingDirectory != null) { e.setWorkingDirectory(this.workingDirectory); final CommandLine cl = new CommandLine(javaExecutable); if (vmOptions != null && vmOptions.length() > 0) { cl.addArgument(option); cl.addArgument("-jar"); cl.addArgument(jarToExecute.getAbsolutePath()); cl.addArgument("-p"); cl.addArgument(String.valueOf(serverPort)); log.info("Executing " + cl); e.setStreamHandler(new PumpStreamHandler()); e.setProcessDestroyer(new ShutdownHookProcessDestroyer()); e.execute(cl, h);
try { logger.trace("Executing: " + fullCommand); CommandLine cli = CommandLine.parse(fullCommand); Executor executor = new DefaultExecutor(); LogOutputStream outStream = new LogOutputStream() { closer.register(outStream); closer.register(errStream); ExecuteStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream); executor.setStreamHandler(streamHandler); executor.setWatchdog(watchDog); int retValue = executor.execute(cli, ENVIRONMENT_MAP); if (retValue != 0) { logger.error(
public void printJavaVersion() { CommandLine cmdLine = new CommandLine("java"); cmdLine.addArgument("-version"); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); Executor executor = new DefaultExecutor(); try { executor.execute(cmdLine, resultHandler); } catch (ExecuteException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
final CommandLine cmdLine = new CommandLine(pd.getCommand()); cmdLine.addArgument(it.next()); Executor executor = new DefaultExecutor(); new ModifiedPumpStreamHandler(pd.getOutputFile()); executor.setStreamHandler(modifiedPumpStreamHandler); executor.setWatchdog(watchdog); executor.setWorkingDirectory(pd.getWorkingDirectory()); executor.execute(cmdLine, getenv, resultHandler);
CommandLine cmdLine = new CommandLine(dotCommand); cmdLine.addArgument("-Tpng"); cmdLine.addArgument("-o"); cmdLine.addArgument(pugImage.getName()); cmdLine.addArgument(pugDOT.getName()); Executor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setWatchdog(watchdog); executor.setWorkingDirectory(pugTemp); executor.execute(cmdLine, resultHandler); if (!executor.isFailure(resultHandler.getExitValue())) { pluginLog.info(String.format("%s - convertDOTgraph - reading image for '%s' from: '%s'", pluginName, project.getName(), pugImage.getName())); BufferedImageFile imageFile = new BufferedImageFile(pugImage);
private void makeDirGroupWritable(final String dir) throws GenieServerException { log.debug("Adding write permissions for the directory {} for the group.", dir); final CommandLine commandLIne = new CommandLine("sudo").addArgument("chmod").addArgument("g+w") .addArgument(dir); try { this.executor.execute(commandLIne); } catch (IOException ioe) { throw new GenieServerException("Could not make the job working logs directory group writable.", ioe); } }
/** * Execute a command on the operating system using Apache Commons Exec. This * function runs asynchronously and dumps both stderr and stdout streams to * a temp file. * * @param commandLine * The command to be executed. * @param outputStreamHandler * An output stream to dump the process stderr and stdout to it. */ public static void runCommandUsingApacheExec(CommandLine commandLine, OutputStream outputStreamHandler) { try { DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStreamHandler); logger.info("commandLine: " + commandLine.toString()); Executor process = new DefaultExecutor(); process.setExitValue(0); process.setStreamHandler(streamHandler); process.execute(commandLine, resultHandler); // resultHandler.waitFor(); } catch (Exception ex) { logger.error("An exception was thrown.", ex); } }
public boolean run(boolean displayCmd, boolean throwFailure) throws Exception { List<String> cmd = buildCommand(); displayCmd(displayCmd, cmd); Executor exec = new DefaultExecutor(); exec.setStreamHandler(new PumpStreamHandler(System.out)); } else { exec.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { CommandLine cl = new CommandLine(cmd.get(0)); for (int i = 1; i < cmd.size(); i++) { cl.addArgument(cmd.get(i)); int exitValue = exec.execute(cl); if (exitValue != 0) { if (throwFailure) {
public void run(Context context) { try (OutputStream out = outStream(context); OutputStream err = errStream(context)) { CommandLine cli = CommandLine .parse(String.format("%s/%s", getProcEnvironment().get(CIRCUS_TRAIN_HOME_ENV_VAR), CIRCUS_TRAIN_HOME_SCRIPT)); cli.addArgument("--config=" + context.getConfigLocation()); if (!StringUtils.isEmpty(context.getCircusTrainConfigLocation())) { cli.addArgument("--config=" + context.getCircusTrainConfigLocation()); } Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(new File(context.getWorkspace())); executor.setStreamHandler(new PumpStreamHandler(out, err)); log.debug("Executing {} with environment {}", cli, getProcEnvironment()); int returnValue = executor.execute(cli, getProcEnvironment()); log.debug("Command exited with value {} ", returnValue); if (returnValue != 0) { throw new CircusTrainException("Circus Train exited with error value " + returnValue); } } catch (Throwable e) { log.error("Unable to execute Circus Train", e); } }
private static Executor getOutputIgnoringExecutor() { Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(null, null)); return executor; } }