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); } }
executor.execute(cmdLine, resultHandler); logger.debug("executed commandLine '{}'", commandLine); } catch (IOException 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()); } }
executor.setStreamHandler(streamHandler); executor.setWatchdog(watchDog); int retValue = executor.execute(cli, ENVIRONMENT_MAP); if (retValue != 0) { logger.error(
/** * {@inheritDoc} */ @Override public void checkProcess() throws GenieTimeoutException, ExecuteException, IOException { this.executor.execute(this.commandLine); // If we get here the process is still running. Check if it should be killed due to timeout. if (new Date().getTime() > this.timeout.getTime()) { throw new GenieTimeoutException( "Job has exceeded its timeout time of " + this.dateFormatter.format(this.timeout) ); } } }
public static Pair<Boolean, String> run(CommandLine command) { ExecuteWatchdog watchDog = new ExecuteWatchdog(30000); StringBuffer output = new StringBuffer(); Executor executor = createExecutor(output::append, command, new File("."), watchDog); output.setLength(0); try { int exitValue = executor.execute(command); if (executor.isFailure(exitValue)) { return Pair.of(false, "Not available"); } } catch (Exception e) { return Pair.of(false, "Not available"); } return Pair.of(true, output.toString().trim()); }
/** * @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); } }
/** * @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); } }
/** * @VisibleForTesting */ void doProcess(final InputStream in, final OutputStream out) throws 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(NGANN_COMMAND)); LOG.debug("result={}", result); if (result != 0) { throw new ExecuteException("Processing failed: " + new String(errorStream.toByteArray()), result); } }
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); } }
public void launch() { Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(createOutputStream())); try { exitCode = executor.execute(commandLine); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Execute the sub-process * * @param executor * The executor to use * @param cmdLine * The command line to execute * @param streamHandler * The stream handler to use * @throws MojoExecutionException * If the process execution fails */ protected void executeTarget(Executor executor, CommandLine cmdLine, PumpStreamHandler streamHandler) throws MojoExecutionException { executor.setStreamHandler(streamHandler); try { executor.execute(cmdLine); } catch (IOException e) { getLog().error("Failed to run " + cmdLine.getExecutable(), e); throw new MojoExecutionException("Failed to run " + cmdLine.getExecutable(), e); } } }
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); } }
/** * 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); } }
/** * Executes the given command with KRB5_CONFIG environment variable pointing to the specialized * config file for the embedded KDC server. */ public static void executeCommand(String command, EmbeddedADS adsServer) throws IOException { Map<String, String> environmentMap = ImmutableMap.<String, String>builder() .put("KRB5_CONFIG", adsServer.getKrb5Conf().getAbsolutePath()) .build(); CommandLine cli = CommandLine.parse(command); Executor executor = new DefaultExecutor(); int retValue = executor.execute(cli, environmentMap); assertThat(retValue).isZero(); }
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 void internalExecute(CommandLine cmdLine, OutputStream outputStream, File workingDirectory) throws IOException { Executor executor = getExecutor(); ExecuteWatchdog watchdog = getExecuteWatchdog(); executor.setWatchdog(watchdog); if (workingDirectory != null) { executor.setWorkingDirectory(workingDirectory); } // set allowed exit values (0 is actually the default) executor.setExitValue(0); PumpStreamHandler psh = new PumpStreamHandler(outputStream); executor.setStreamHandler(psh); executor.execute(cmdLine); if (watchdog.killedProcess()) { throw new ExecuteException(String.format("Watchdog killed Sencha Cmd process after %s ms.", MAX_EXECUTION_TIME), 0); } }
/** * Method to change the ownership of a directory. * * @param dir The directory to change the ownership of. * @param user Userid of the user. * @throws GenieException If there is a problem. */ protected void changeOwnershipOfDirectory( final String dir, final String user) throws GenieException { final CommandLine commandLine = new CommandLine("sudo").addArgument("chown").addArgument("-R") .addArgument(user).addArgument(dir); try { this.executor.execute(commandLine); } catch (IOException ioexception) { throw new GenieServerException("Could not change ownership", ioexception); } }
public static void run(LineConsumer outputHandler, Map<String, String> envVarsForApp, CommandLine command, File projectRoot, long timeout) throws ProjectCannotStartException { long startTime = logStartInfo(command, projectRoot); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout); Executor executor = createExecutor(outputHandler, command, projectRoot, watchDog); try { int exitValue = executor.execute(command, envVarsForApp); if (executor.isFailure(exitValue)) { String message = watchDog.killedProcess() ? "Timed out waiting for " + command : "Exit code " + exitValue + " returned from " + command; throw new ProjectCannotStartException(message); } } catch (Exception e) { String message = "Error running: " + fullPath(projectRoot) + "> " + StringUtils.join(command.toStrings(), " "); outputHandler.consumeLine(message); outputHandler.consumeLine(e.toString()); throw new ProjectCannotStartException(message, e); } logEndTime(command, startTime); }