Search in sources :

Example 11 with DefaultExecutor

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

the class Os method shell.

public void shell(File dir, String cmd) throws Exception {
    CommandLine commandLine;
    if (s_windows) {
        commandLine = new CommandLine("cmd");
        commandLine.addArgument("/C");
        commandLine.addArgument(cmd, false);
    } else {
        commandLine = new CommandLine("/bin/bash");
        commandLine.addArgument("-c");
        commandLine.addArgument(cmd, false);
    }
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(dir);
    executor.setStreamHandler(new PumpStreamHandler(System.out, System.out));
    executor.execute(commandLine);
}
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)

Example 12 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project xwiki-platform by xwiki.

the class XWikiExecutor method executeCommand.

private DefaultExecuteResultHandler executeCommand(String commandLine) throws Exception {
    // The command line to execute
    CommandLine command = CommandLine.parse(commandLine);
    // Execute the process asynchronously so that we don't block.
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    // Send Process output and error streams to our logger.
    PumpStreamHandler streamHandler = new PumpStreamHandler(new XWikiLogOutputStream(XWikiLogOutputStream.STDOUT), new XWikiLogOutputStream(XWikiLogOutputStream.STDERR));
    // Make sure we end the process when the JVM exits
    ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
    // Prevent the process from running indefinitely and kill it after 1 hour...
    ExecuteWatchdog watchDog = new ExecuteWatchdog(60L * 60L * 1000L);
    // The executor to execute the command
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(streamHandler);
    executor.setWorkingDirectory(new File(getExecutionDirectory()));
    executor.setProcessDestroyer(processDestroyer);
    executor.setWatchdog(watchDog);
    // Inherit the current process's environment variables and add the user-defined ones
    @SuppressWarnings("unchecked") Map<String, String> newEnvironment = EnvironmentUtils.getProcEnvironment();
    newEnvironment.putAll(this.environment);
    executor.execute(command, newEnvironment, resultHandler);
    return resultHandler;
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) File(java.io.File) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 13 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project xxl-job by xuxueli.

the class ScriptUtil method execToFile.

/**
 * 日志文件输出方式
 *
 * 优点:支持将目标数据实时输出到指定日志文件中去
 * 缺点:
 *      标准输出和错误输出优先级固定,可能和脚本中顺序不一致
 *      Java无法实时获取
 *
 * @param command
 * @param scriptFile
 * @param logFile
 * @param params
 * @return
 * @throws IOException
 */
public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
    // 标准输入
    try (FileOutputStream fileOutputStream = new FileOutputStream(logFile, true)) {
        PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);
        // command
        CommandLine commandline = new CommandLine(command);
        commandline.addArgument(scriptFile);
        if (params != null && params.length > 0) {
            commandline.addArguments(params);
        }
        // exec
        DefaultExecutor exec = new DefaultExecutor();
        exec.setExitValues(null);
        exec.setStreamHandler(streamHandler);
        // exit code: 0=success, 1=error
        int exitValue = exec.execute(commandline);
        return exitValue;
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) FileOutputStream(java.io.FileOutputStream)

Example 14 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project scala-maven-plugin by davidB.

the class JavaMainCallerByFork method run.

@Override
public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
    List<String> cmd = buildCommand();
    displayCmd(displayCmd, cmd);
    Executor exec = new DefaultExecutor();
    // err and out are redirected to out
    if (!_redirectToLog) {
        exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in));
    } else {
        exec.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {

            private LevelState _previous = new LevelState();

            @Override
            protected void processLine(String line, int level) {
                try {
                    _previous = LogProcessorUtils.levelStateOf(line, _previous);
                    switch(_previous.level) {
                        case ERROR:
                            requester.getLog().error(line);
                            break;
                        case WARNING:
                            requester.getLog().warn(line);
                            break;
                        default:
                            requester.getLog().info(line);
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }));
    }
    CommandLine cl = new CommandLine(cmd.get(0));
    for (int i = 1; i < cmd.size(); i++) {
        cl.addArgument(cmd.get(i), false);
    }
    try {
        int exitValue = exec.execute(cl);
        if (exitValue != 0) {
            if (throwFailure) {
                throw new MojoFailureException("command line returned non-zero value:" + exitValue);
            }
            return false;
        }
        if (!displayCmd)
            tryDeleteArgFile(cmd);
        return true;
    } catch (ExecuteException exc) {
        if (throwFailure) {
            throw exc;
        }
        return false;
    }
}
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) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ExecuteException(org.apache.commons.exec.ExecuteException) LevelState(scala_maven_executions.LogProcessorUtils.LevelState) LogOutputStream(org.apache.commons.exec.LogOutputStream) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 15 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project zalenium by zalando.

the class CommonProxyUtilities method convertFlvFileToMP4.

public void convertFlvFileToMP4(TestInformation testInformation) {
    // Names of the old and the new file
    String flvVideoFile = testInformation.getVideoFolderPath() + "/" + testInformation.getFileName();
    testInformation.setFileExtension(".mp4");
    testInformation.buildVideoFileName();
    String mp4VideoFile = testInformation.getVideoFolderPath() + "/" + testInformation.getFileName();
    // Command to convert the file to MP4
    CommandLine commandLine = new CommandLine("ffmpeg");
    commandLine.addArgument("-i");
    commandLine.addArgument(flvVideoFile);
    commandLine.addArgument(mp4VideoFile);
    DefaultExecutor defaultExecutor = new DefaultExecutor();
    ExecuteWatchdog executeWatchdog = new ExecuteWatchdog(10 * 1000);
    defaultExecutor.setWatchdog(executeWatchdog);
    try {
        int exitValue = defaultExecutor.execute(commandLine);
        if (exitValue != 0) {
            LOG.warn("File " + flvVideoFile + " could not be converted to MP4. Exit value: " + exitValue);
        }
    } catch (IOException e) {
        LOG.error(e.toString(), e);
    }
    // Deleting the FLV file
    FileUtils.deleteQuietly(new File(flvVideoFile));
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) IOException(java.io.IOException) File(java.io.File)

Aggregations

DefaultExecutor (org.apache.commons.exec.DefaultExecutor)82 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)34 ExecuteException (org.apache.commons.exec.ExecuteException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 File (java.io.File)25 Executor (org.apache.commons.exec.Executor)12 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