Search in sources :

Example 26 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project evosuite by EvoSuite.

the class ProcessLauncher method launchNewProcess.

public int launchNewProcess(File baseDir, String cmdString, int timeout) throws IOException, ProcessTimeoutException {
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog timeoutWatchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(timeoutWatchdog);
    PumpStreamHandler streamHandler = new PumpStreamHandler(this.outAndErr, this.outAndErr, this.input);
    executor.setStreamHandler(streamHandler);
    if (baseDir != null) {
        executor.setWorkingDirectory(baseDir);
    }
    int exitValue;
    try {
        logger.debug("About to execute command " + cmdString);
        exitValue = executor.execute(CommandLine.parse(cmdString));
        if (executor.isFailure(exitValue) && timeoutWatchdog.killedProcess()) {
            // it was killed on purpose by the watchdog
            logger.debug("A timeout occured while executing a process");
            logger.debug("The command is " + cmdString);
            throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString);
        }
        return exitValue;
    } catch (ExecuteException e) {
        if (timeoutWatchdog.killedProcess()) {
            logger.debug("A timeout occured while executing a process");
            logger.debug("The command is " + cmdString);
            throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString);
        } else {
            throw e;
        }
    }
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 27 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project mule by mulesoft.

the class AbstractOSController method executeSyncCommand.

private int executeSyncCommand(String command, String[] args, Map<Object, Object> newEnv, int timeout) throws MuleControllerException {
    CommandLine commandLine = new CommandLine(muleBin);
    commandLine.addArgument(command);
    commandLine.addArguments(args);
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(new PumpStreamHandler());
    return doExecution(executor, commandLine, newEnv);
}
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)

Example 28 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project mule by mulesoft.

the class MuleUtils method executeCommand.

public static int executeCommand(String command, String... envVars) throws IOException {
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    Map<String, String> env = addEnvProperties(envVars);
    ExecuteWatchdog watchDog = new ExecuteWatchdog(TIMEOUT);
    executor.setWatchdog(watchDog);
    executor.setStreamHandler(new PumpStreamHandler());
    int result = executor.execute(cmdLine, env);
    if (executor.isFailure(result)) {
        if (watchDog.killedProcess()) {
            throw new RuntimeException("Reached timeout while running: " + cmdLine);
        }
        throw new RuntimeException("Process failed with return code [" + result + "]: " + cmdLine);
    }
    return result;
}
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)

Example 29 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project fmv by f-agu.

the class FMVExecutor method setTimeOut.

/**
 * @param timeOutMilliSeconds
 */
public void setTimeOut(long timeOutMilliSeconds) {
    if (timeOutMilliSeconds >= 0 && timeOutMilliSeconds < 10) {
        throw new IllegalArgumentException("Incredible timeout: " + timeOutMilliSeconds + "ms");
    }
    this.timeOutMilliSeconds = Math.max(timeOutMilliSeconds, org.apache.commons.exec.ExecuteWatchdog.INFINITE_TIMEOUT);
    executeWatchdog = new ExecuteWatchdog(timeOutMilliSeconds);
    setWatchdog(executeWatchdog);
}
Also used : ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog)

Example 30 with ExecuteWatchdog

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

the class MongoDbInterpreter method stopProcess.

private void stopProcess(String paragraphId) {
    if (runningProcesses.containsKey(paragraphId)) {
        final Executor executor = runningProcesses.get(paragraphId);
        final ExecuteWatchdog watchdog = executor.getWatchdog();
        watchdog.destroyProcess();
        runningProcesses.remove(paragraphId);
    }
}
Also used : Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog)

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