List of usage examples for org.apache.commons.exec DefaultExecuteResultHandler getExitValue
public int getExitValue()
From source file:ro.cosu.vampires.client.executors.fork.ForkExecutor.java
@Override public Result execute(Computation computation) { acquireResources();//from w ww. ja v a 2s.c o m CommandLine commandLine = getCommandLine(computation.command()); CollectingLogOutputStream collectingLogOutputStream = new CollectingLogOutputStream(); PumpStreamHandler handler = new PumpStreamHandler(collectingLogOutputStream); executor.setStreamHandler(handler); executor.setWatchdog(new ExecuteWatchdog(TIMEOUT_IN_MILIS)); executor.setWorkingDirectory(Paths.get("").toAbsolutePath().toFile()); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); LocalDateTime start = LocalDateTime.now(); int exitCode; try { executor.execute(commandLine, resultHandler); } catch (IOException e) { LOG.error("failed to exec", resultHandler.getException()); } try { resultHandler.waitFor(); } catch (InterruptedException e) { LOG.error("failed to exec", resultHandler.getException()); } exitCode = resultHandler.hasResult() ? resultHandler.getExitValue() : -1; //TODO take different action for failed commands so we can collect the output (stderr or java exception) LocalDateTime stop = LocalDateTime.now(); long duration = Duration.between(start, stop).toMillis(); releaseResources(); return Result.builder().duration(duration).exitCode(exitCode).trace(getTrace(start, stop)) .output(collectingLogOutputStream.getLines()).build(); }
From source file:sce.ProcessExecutor.java
public String executeProcess(String[] processParameters) throws JobExecutionException { try {//from w w w. jav a 2 s .c o m //Command to be executed CommandLine command = new CommandLine(processParameters[0]); String[] params = new String[processParameters.length - 1]; for (int i = 0; i < processParameters.length - 1; i++) { params[i] = processParameters[i + 1]; } //Adding its arguments command.addArguments(params); //set timeout in seconds ExecuteWatchdog watchDog = new ExecuteWatchdog( this.timeout == 0 ? ExecuteWatchdog.INFINITE_TIMEOUT : this.timeout * 1000); this.watchdog = watchDog; //Result Handler for executing the process in a Asynch way DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); //MyResultHandler resultHandler = new MyResultHandler(); //Using Std out for the output/error stream //ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); //This is used to end the process when the JVM exits ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer(); //Our main command executor DefaultExecutor executor = new DefaultExecutor(); //Setting the properties executor.setStreamHandler(new PumpStreamHandler(null, null)); executor.setWatchdog(watchDog); //executor.setExitValue(1); // this has to be set if the java code contains System.exit(1) to avoid a FAILED status //Setting the working directory //Use of recursion along with the ls makes this a long running process //executor.setWorkingDirectory(new File("/home")); executor.setProcessDestroyer(processDestroyer); //if set, use the java environment variables when running the command if (!this.environment.equals("")) { Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment(); EnvironmentUtils.addVariableToEnvironment(procEnv, this.environment); //Executing the command executor.execute(command, procEnv, resultHandler); } else { //Executing the command executor.execute(command, resultHandler); } //The below section depends on your need //Anything after this will be executed only when the command completes the execution resultHandler.waitFor(); /*int exitValue = resultHandler.getExitValue(); System.out.println(exitValue); if (executor.isFailure(exitValue)) { System.out.println("Execution failed"); } else { System.out.println("Execution Successful"); } System.out.println(outputStream.toString());*/ //return outputStream.toString(); if (watchdog.killedProcess()) { throw new JobExecutionException("Job Interrupted", new InterruptedException()); } if (executor.isFailure(resultHandler.getExitValue())) { ExecuteException ex = resultHandler.getException(); throw new JobExecutionException(ex.getMessage(), ex); } return "1"; } catch (ExecuteException ex) { throw new JobExecutionException(ex.getMessage(), ex); } catch (IOException | InterruptedException | JobExecutionException ex) { throw new JobExecutionException(ex.getMessage(), ex); } }
From source file:uk.org.sappho.applications.transcript.service.registry.vcs.CommandExecuter.java
public String execute(Command command, File directory) throws TranscriptException { try {// w w w . j a va2s . co m if (logger.isLoggable(Level.INFO)) { logger.info(command.getSafeCommand()); } DefaultExecutor executor = new DefaultExecutor(); DefaultExecuteResultHandler commandResultsHandler = new DefaultExecuteResultHandler(); ByteArrayOutputStream commandOutputStream = new ByteArrayOutputStream(); ByteArrayOutputStream commandErrorStream = new ByteArrayOutputStream(); PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(commandOutputStream, commandErrorStream); executor.setWatchdog(new ExecuteWatchdog(60000)); executor.setStreamHandler(pumpStreamHandler); executor.setWorkingDirectory(directory); executor.execute(command.getCommandLine(), commandResultsHandler); commandResultsHandler.waitFor(); if (commandResultsHandler.getExitValue() != 0) { throw new TranscriptException(commandErrorStream.toString()); } return commandOutputStream.toString(); } catch (Throwable throwable) { if (logger.isLoggable(Level.WARNING)) { logger.warning(throwable.getMessage()); } throw new TranscriptException("Unable to execute system command: " + command.getSafeCommand(), throwable); } }