Search in sources :

Example 36 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project frontend-maven-plugin by eirslett.

the class ProcessExecutor method createExecutor.

private Executor createExecutor(File workingDirectory, long timeoutInSeconds) {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(workingDirectory);
    // Fixes #41
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    if (timeoutInSeconds > 0) {
        executor.setWatchdog(new ExecuteWatchdog(timeoutInSeconds * 1000));
    }
    return executor;
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 37 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project smarthome by eclipse.

the class ExecUtil method executeCommandLineAndWaitResponse.

/**
 * <p>
 * Executes <code>commandLine</code>. Sometimes (especially observed on MacOS) the commandLine isn't executed
 * properly. In that cases another exec-method is to be used. To accomplish this please use the special delimiter '
 * <code>@@</code>'. If <code>commandLine</code> contains this delimiter it is split into a String[] array and the
 * special exec-method is used.
 *
 * <p>
 * A possible {@link IOException} gets logged but no further processing is done.
 *
 * @param commandLine the command line to execute
 * @param timeout timeout for execution in milliseconds
 * @return response data from executed command line
 */
public static String executeCommandLineAndWaitResponse(String commandLine, int timeout) {
    String retval = null;
    CommandLine cmdLine = null;
    if (commandLine.contains(CMD_LINE_DELIMITER)) {
        String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER);
        cmdLine = new CommandLine(cmdArray[0]);
        for (int i = 1; i < cmdArray.length; i++) {
            cmdLine.addArgument(cmdArray[i], false);
        }
    } else {
        cmdLine = CommandLine.parse(commandLine);
    }
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    Executor executor = new DefaultExecutor();
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout);
    executor.setExitValues(null);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(watchdog);
    try {
        executor.execute(cmdLine, resultHandler);
        logger.debug("executed commandLine '{}'", commandLine);
    } catch (ExecuteException e) {
        logger.warn("couldn't execute commandLine '{}'", commandLine, e);
    } catch (IOException e) {
        logger.warn("couldn't execute commandLine '{}'", commandLine, e);
    }
    // can safely request the exit code
    try {
        resultHandler.waitFor();
        int exitCode = resultHandler.getExitValue();
        retval = StringUtils.chomp(stdout.toString());
        if (resultHandler.getException() != null) {
            logger.warn("{}", resultHandler.getException().getMessage());
        } else {
            logger.debug("exit code '{}', result '{}'", exitCode, retval);
        }
    } catch (InterruptedException e) {
        logger.warn("Timeout occurred when executing commandLine '{}'", commandLine, e);
    }
    return retval;
}
Also used : DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException)

Aggregations

ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)37 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)36 CommandLine (org.apache.commons.exec.CommandLine)25 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)24 IOException (java.io.IOException)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 ExecuteException (org.apache.commons.exec.ExecuteException)14 File (java.io.File)12 Executor (org.apache.commons.exec.Executor)10 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)8 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)7 HashMap (java.util.HashMap)4 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)3 Code (org.apache.zeppelin.interpreter.InterpreterResult.Code)3 Jinjava (com.hubspot.jinjava.Jinjava)2 DataInputStream (java.io.DataInputStream)2 FileOutputStream (java.io.FileOutputStream)2