Search in sources :

Example 1 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project zeppelin by apache.

the class ShellInterpreter method interpret.

@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
    LOGGER.debug("Run shell command '" + cmd + "'");
    OutputStream outStream = new ByteArrayOutputStream();
    CommandLine cmdLine = CommandLine.parse(shell);
    // they need to be delimited by '&&' instead
    if (isWindows) {
        String[] lines = StringUtils.split(cmd, "\n");
        cmd = StringUtils.join(lines, " && ");
    }
    cmdLine.addArgument(cmd, false);
    try {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(contextInterpreter.out, contextInterpreter.out));
        executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
        executors.put(contextInterpreter.getParagraphId(), executor);
        int exitVal = executor.execute(cmdLine);
        LOGGER.info("Paragraph " + contextInterpreter.getParagraphId() + " return with exit value: " + exitVal);
        return new InterpreterResult(Code.SUCCESS, outStream.toString());
    } catch (ExecuteException e) {
        int exitValue = e.getExitValue();
        LOGGER.error("Can not run " + cmd, e);
        Code code = Code.ERROR;
        String message = outStream.toString();
        if (exitValue == 143) {
            code = Code.INCOMPLETE;
            message += "Paragraph received a SIGTERM\n";
            LOGGER.info("The paragraph " + contextInterpreter.getParagraphId() + " stopped executing: " + message);
        }
        message += "ExitValue: " + exitValue;
        return new InterpreterResult(code, message);
    } catch (IOException e) {
        LOGGER.error("Can not run " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } finally {
        executors.remove(contextInterpreter.getParagraphId());
    }
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 2 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project hive by apache.

the class ExecServiceImpl method auxRun.

private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(null);
    // Setup stdout and stderr
    int nbytes = appConf.getInt(AppConfig.EXEC_MAX_BYTES_NAME, -1);
    ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes);
    ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes);
    executor.setStreamHandler(new PumpStreamHandler(outStream, errStream));
    // Only run for N milliseconds
    int timeout = appConf.getInt(AppConfig.EXEC_TIMEOUT_NAME, 0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    CommandLine cmd = makeCommandLine(program, args);
    LOG.info("Running: " + cmd);
    ExecBean res = new ExecBean();
    res.exitcode = executor.execute(cmd, execEnv(env));
    String enc = appConf.get(AppConfig.EXEC_ENCODING_NAME);
    res.stdout = outStream.toString(enc);
    res.stderr = errStream.toString(enc);
    try {
        watchdog.checkException();
    } catch (Exception ex) {
        LOG.error("Command: " + cmd + " failed. res=" + res, ex);
    }
    if (watchdog.killedProcess()) {
        String msg = " was terminated due to timeout(" + timeout + "ms).  See " + AppConfig.EXEC_TIMEOUT_NAME + " property";
        LOG.warn("Command: " + cmd + msg + " res=" + res);
        res.stderr += " Command " + msg;
    }
    if (res.exitcode != 0) {
        LOG.info("Command: " + cmd + " failed. res=" + res);
    }
    return res;
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 3 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project camel by apache.

the class DefaultExecCommandExecutor method prepareDefaultExecutor.

protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) {
    DefaultExecutor executor = new ExecDefaultExecutor();
    executor.setExitValues(null);
    if (execCommand.getWorkingDir() != null) {
        executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile());
    }
    if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) {
        executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout()));
    }
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    return executor;
}
Also used : ExecDefaultExecutor(org.apache.camel.component.exec.ExecDefaultExecutor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) File(java.io.File) ExecDefaultExecutor(org.apache.camel.component.exec.ExecDefaultExecutor) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 4 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project robovm by robovm.

the class Executor method execCapture.

public String execCapture() throws IOException {
    ExecuteStreamHandler oldStreamHandler = streamHandler;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CommandLine commandLine = generateCommandLine();
    try {
        streamHandler(new PumpStreamHandler(baos));
        logCommandLine(commandLine);
        DefaultExecutor executor = initExecutor(new DefaultExecutor());
        executor.execute(commandLine, generateEnv());
        return new String(baos.toByteArray()).trim();
    } catch (ExecuteException e) {
        String output = new String(baos.toByteArray()).trim();
        if (output.length() > 0 && e.getMessage().startsWith("Process exited with an error")) {
            StackTraceElement[] origStackTrace = e.getStackTrace();
            e = new ExecuteException("Command '" + commandLine + "' failed with output: " + output + " ", e.getExitValue());
            e.setStackTrace(origStackTrace);
        }
        throw e;
    } finally {
        streamHandler = oldStreamHandler;
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteStreamHandler(org.apache.commons.exec.ExecuteStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project kylo by Teradata.

the class MultiUserProcessManager method refreshKerberosTicket.

/**
 * Calls kinit to request a new Kerberos ticket if the previous one is about to expire.
 */
private void refreshKerberosTicket() {
    // Determine if a new ticket is needed
    if (kerberos == null || kerberos.getInitInterval() <= 0 || kerberosNextInit > DateTimeUtils.currentTimeMillis()) {
        return;
    }
    // Build executor
    final Executor executor = new DefaultExecutor();
    final ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
    executor.setProcessDestroyer(processDestroyer);
    final Logger outputLogger = LoggerFactory.getLogger(getClass().getName() + ".kinit");
    final LoggerOutputStream outputStream = new LoggerOutputStream(outputLogger);
    final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    final ExecuteWatchdog watchdog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(kerberos.getInitTimeout()));
    executor.setWatchdog(watchdog);
    // Run kinit to acquire a new ticket
    final CommandLine command = new CommandLine("kinit").addArgument("-kt").addArgument(kerberos.getKeytabLocation()).addArgument(kerberos.getKerberosPrincipal());
    log.debug("Acquiring a new Kerberos ticket with command: {}", command);
    int exitCode;
    try {
        exitCode = executor.execute(command);
    } catch (final IOException e) {
        log.error("Failed to execute kinit", e);
        exitCode = -1;
    }
    // Record next time to acquire ticket
    if (!executor.isFailure(exitCode)) {
        kerberosNextInit = DateTimeUtils.currentTimeMillis() + TimeUnit.SECONDS.toMillis(kerberos.getInitInterval());
    } else {
        if (watchdog.killedProcess()) {
            log.error("Failed to acquire a Kerberos ticket within the allotted time: {}", kerberos.getInitTimeout());
        } else {
            log.error("Kinit exited with non-zero status: {}", exitCode);
        }
        kerberosNextInit = DateTimeUtils.currentTimeMillis() + TimeUnit.SECONDS.toMillis(kerberos.getRetryInterval());
        throw new IllegalStateException("Failed to acquire a Kerberos ticket");
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) IOException(java.io.IOException) Logger(org.slf4j.Logger) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Aggregations

DefaultExecutor (org.apache.commons.exec.DefaultExecutor)81 CommandLine (org.apache.commons.exec.CommandLine)62 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)47 IOException (java.io.IOException)36 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)33 ExecuteException (org.apache.commons.exec.ExecuteException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 File (java.io.File)24 Executor (org.apache.commons.exec.Executor)11 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)9 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)9 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 URL (java.net.URL)4 Properties (java.util.Properties)4 LogOutputStream (org.apache.commons.exec.LogOutputStream)4 InputStream (java.io.InputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3 HashSet (java.util.HashSet)3