Search in sources :

Example 6 with CommandLine

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

the class ProcessExecutor method createCommandLine.

private CommandLine createCommandLine(List<String> command) {
    CommandLine commmandLine = new CommandLine(command.get(0));
    for (int i = 1; i < command.size(); i++) {
        String argument = command.get(i);
        commmandLine.addArgument(argument, false);
    }
    return commmandLine;
}
Also used : CommandLine(org.apache.commons.exec.CommandLine)

Example 7 with CommandLine

use of org.apache.commons.exec.CommandLine 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 8 with CommandLine

use of org.apache.commons.exec.CommandLine 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 9 with CommandLine

use of org.apache.commons.exec.CommandLine in project elastic-job by dangdangdotcom.

the class TaskLaunchScheduledService method buildCommand.

private Protos.CommandInfo buildCommand(final Protos.CommandInfo.URI uri, final String bootstrapScript, final ShardingContexts shardingContexts, final boolean useDefaultExecutor) {
    Protos.CommandInfo.Builder result = Protos.CommandInfo.newBuilder().addUris(uri).setShell(true);
    if (useDefaultExecutor) {
        CommandLine commandLine = CommandLine.parse(bootstrapScript);
        commandLine.addArgument(GsonFactory.getGson().toJson(shardingContexts), false);
        result.setValue(Joiner.on(" ").join(commandLine.getExecutable(), Joiner.on(" ").join(commandLine.getArguments())));
    } else {
        result.setValue(bootstrapScript);
    }
    return result.build();
}
Also used : CommandLine(org.apache.commons.exec.CommandLine)

Example 10 with CommandLine

use of org.apache.commons.exec.CommandLine in project opennms by OpenNMS.

the class MeasurementsRestServiceITCase method assumeRrdtoolExists.

protected static void assumeRrdtoolExists(final String libraryName) {
    final String libraryPath = System.getProperty("java.library.path", "");
    if (!libraryPath.contains(":/usr/local/lib")) {
        System.setProperty("java.library.path", libraryPath + ":/usr/local/lib");
    }
    boolean rrdtoolExists = false;
    try {
        final CommandLauncher cl = CommandLauncherFactory.createVMLauncher();
        final Process p = cl.exec(new CommandLine(findRrdtool()), EnvironmentUtils.getProcEnvironment());
        final int returnCode = p.waitFor();
        LOG.debug("Loading library from java.library.path={}", System.getProperty("java.library.path"));
        System.loadLibrary(libraryName);
        rrdtoolExists = returnCode == 0;
    } catch (final Exception e) {
        LOG.warn("Failed to run 'rrdtool' or libjrrd(2)? is missing.", e);
    }
    Assume.assumeTrue(rrdtoolExists);
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) CommandLauncher(org.apache.commons.exec.launcher.CommandLauncher) ExpectedException(org.junit.rules.ExpectedException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

CommandLine (org.apache.commons.exec.CommandLine)44 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)26 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)20 IOException (java.io.IOException)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 ExecuteException (org.apache.commons.exec.ExecuteException)12 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)11 File (java.io.File)8 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)4 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)4 InputStream (java.io.InputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Map (java.util.Map)3 Executor (org.apache.commons.exec.Executor)3 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2