Search in sources :

Example 51 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project x-pipe by ctripcorp.

the class AbstractRedisTest method executeScript.

protected void executeScript(String file, String... args) throws ExecuteException, IOException {
    URL url = getClass().getClassLoader().getResource(file);
    if (url == null) {
        url = getClass().getClassLoader().getResource("scripts/" + file);
        if (url == null) {
            throw new FileNotFoundException(file);
        }
    }
    DefaultExecutor executor = new DefaultExecutor();
    String command = "sh -v " + url.getFile() + " " + StringUtil.join(" ", args);
    executor.execute(CommandLine.parse(command));
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL)

Example 52 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project x-pipe by ctripcorp.

the class AbstractRedisTest method executeCommands.

protected void executeCommands(String... args) throws ExecuteException, IOException {
    DefaultExecutor executor = new DefaultExecutor();
    executor.execute(CommandLine.parse(StringUtil.join(" ", args)));
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor)

Example 53 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project vespa by vespa-engine.

the class ProcessExecutor method execute.

/**
 * Executes the given command synchronously.
 *
 * @param command The command to execute.
 * @param processInput Input provided to the process.
 * @return The result of the execution, or empty if the process does not terminate within the timeout set for this executor.
 * @throws IOException if the process execution failed.
 */
public Optional<ProcessResult> execute(String command, String processInput) throws IOException {
    ByteArrayOutputStream processErr = new ByteArrayOutputStream();
    ByteArrayOutputStream processOut = new ByteArrayOutputStream();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(createStreamHandler(processOut, processErr, processInput));
    ExecuteWatchdog watchDog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(timeoutSeconds));
    executor.setWatchdog(watchDog);
    executor.setExitValues(successExitCodes);
    int exitCode;
    try {
        exitCode = executor.execute(CommandLine.parse(command));
    } catch (ExecuteException e) {
        exitCode = e.getExitValue();
    }
    return (watchDog.killedProcess()) ? Optional.empty() : Optional.of(new ProcessResult(exitCode, processOut.toString(), processErr.toString()));
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ExecuteException(org.apache.commons.exec.ExecuteException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 54 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project fql by CategoricalData.

the class ProcPragma method execute.

@Override
public void execute() {
    try {
        for (String cmd : cmds) {
            // CommandLine.
            CommandLine cmdLine = CommandLine.parse(cmd);
            DefaultExecutor executor = new DefaultExecutor();
            executor.setStreamHandler(new ProcHandler(cmd));
            executor.execute(cmdLine);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException)

Example 55 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project jackrabbit-oak by apache.

the class HybridIndexTest method dumpOpenFilePaths.

private static void dumpOpenFilePaths() throws IOException {
    String jvmName = ManagementFactory.getRuntimeMXBean().getName();
    String pid = jvmName.split("@")[0];
    CommandLine cl = new CommandLine("/bin/sh");
    cl.addArguments(new String[] { "-c", "lsof -p " + pid + " | grep '/nrt'" }, false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(baos));
    executor.execute(cl);
    System.out.println(new String(baos.toByteArray()));
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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