public static Result runCommand(String[] args, Map<String, String> vars, InteractiveHandler interactiveHandler, boolean sudo)
      throws IOException, InterruptedException {

    String[] processArgs;

    if (sudo) {
      processArgs = new String[args.length + 1];
      processArgs[0] = AMBARI_SUDO;
      System.arraycopy(args, 0, processArgs, 1, args.length);
    } else {
      processArgs = args;
    }

    ProcessBuilder builder = new ProcessBuilder(processArgs);

    if (vars != null) {
      Map<String, String> env = builder.environment();
      env.putAll(vars);
    }

    LOG.debug("Executing the command {}", Arrays.toString(processArgs));

    Process process;
    if (WINDOWS) {
      synchronized (WindowsProcessLaunchLock) {
        // To workaround the race condition issue with child processes
        // inheriting unintended handles during process launch that can
        // lead to hangs on reading output and error streams, we
        // serialize process creation. More info available at:
        // http://support.microsoft.com/kb/315939
        process = builder.start();
      }
    } else {
      process = builder.start();
    }

    // If an interactiveHandler is supplied ask it for responses to queries from the command
    // using the InputStream and OutputStream retrieved from the Process object. Use the remainder
    // of the data from the InputStream as the data for stdout.
    InputStream inputStream = process.getInputStream();
    if (interactiveHandler != null) {
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

      interactiveHandler.start();

      try {
        while (!interactiveHandler.done()) {
          StringBuilder query = new StringBuilder();

          while (reader.ready()) {
            query.append((char) reader.read());
          }

          String response = interactiveHandler.getResponse(query.toString());

            if (response != null) {
              writer.write(response);
              writer.newLine();
              writer.flush();
            }
        }
      } catch (IOException ex){
        // ignore exception as command possibly can be finished before writer.flush() or writer.write() called
      } finally {
        writer.close();
      }
    }

    //TODO: not sure whether output buffering will work properly
    // if command output is too intensive
    process.waitFor();
    String stdout = streamToString(inputStream);
    String stderr = streamToString(process.getErrorStream());
    int exitCode = process.exitValue();
    return new Result(exitCode, stdout, stderr);
  }
Runs a command with a given set of environment variables

@param args               a String[] of the command and its arguments
@param vars               a Map of String,String setting an environment variable to run the command with
@param interactiveHandler a handler to provide responses to queries from the command,
                          or null if no queries are expected
@param sudo               true to execute the command using sudo (ambari-sudo); otherwise false
@return Result
@throws IOException
@throws InterruptedException
ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java