private ShellResult execute()

in bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/shell/ShellExecutor.java [185:261]


    private ShellResult execute() throws IOException {
        int exitCode;
        ProcessBuilder builder = new ProcessBuilder(command);
        isTimeout = new AtomicBoolean(false);
        completed = new AtomicBoolean(false);

        if (environment != null) {
            builder.environment().putAll(this.environment);
        }
        if (dir != null) {
            builder.directory(this.dir);
        }

        process = builder.start();
        ProcessContainer.putProcess(process);

        if (timeoutInterval > 0) {
            scheduleTimeoutTimer();
        }

        // read error and input streams as this would free up the buffers
        // free the error stream buffer
        BufferedReader errReader = createBufferedReader(process.getErrorStream());
        StringBuilder errMsg = new StringBuilder();
        Thread errThread = createReaderThread(errReader, errMsg, log::error);

        BufferedReader inReader = createBufferedReader(process.getInputStream());
        StringBuilder inMsg = new StringBuilder();
        Thread inThread = createReaderThread(inReader, inMsg, log::info);

        try {
            errThread.start();
            inThread.start();
        } catch (IllegalStateException ise) {
            log.warn("Illegal while starting the error and in thread", ise);
        }

        try {
            exitCode = process.waitFor();
            try {
                errThread.join();
                inThread.join();
            } catch (InterruptedException ie) {
                log.warn("Interrupted while reading the error and in stream", ie);
            }

            completed.compareAndSet(false, true);
        } catch (InterruptedException ie) {
            throw new IOException(ie.toString());
        } finally {
            if ((timeoutTimer != null) && !isTimeout.get()) {
                timeoutTimer.cancel();
            }

            // close the input stream
            try {
                inReader.close();
            } catch (IOException ioe) {
                log.warn("Error while closing the input stream", ioe);
            }

            if (!completed.get()) {
                errThread.interrupt();
            }

            try {
                errReader.close();
            } catch (IOException ioe) {
                log.warn("Error while closing the error stream", ioe);
            }

            ProcessContainer.removeProcess(process);
            process.destroy();
        }

        return new ShellResult(exitCode, inMsg.toString(), errMsg.toString());
    }