Class Shell.ShellCommandExecutor

java.lang.Object
org.apache.hadoop.util.Shell
org.apache.hadoop.util.Shell.ShellCommandExecutor
All Implemented Interfaces:
Shell.CommandExecutor
Enclosing class:
Shell

public static class Shell.ShellCommandExecutor extends Shell implements Shell.CommandExecutor
A simple shell command executor. ShellCommandExecutorshould be used in cases where the output of the command needs no explicit parsing and where the command, working directory and the environment remains unchanged. The output of the command is stored as-is and is expected to be small.
  • Constructor Details

    • ShellCommandExecutor

      public ShellCommandExecutor(String[] execString)
    • ShellCommandExecutor

      public ShellCommandExecutor(String[] execString, File dir)
    • ShellCommandExecutor

      public ShellCommandExecutor(String[] execString, File dir, Map<String,String> env)
    • ShellCommandExecutor

      public ShellCommandExecutor(String[] execString, File dir, Map<String,String> env, long timeout)
    • ShellCommandExecutor

      public ShellCommandExecutor(String[] execString, File dir, Map<String,String> env, long timeout, boolean inheritParentEnv)
      Create a new instance of the ShellCommandExecutor to execute a command.
      Parameters:
      execString - The command to execute with arguments
      dir - If not-null, specifies the directory which should be set as the current working directory for the command. If null, the current working directory is not modified.
      env - If not-null, environment of the command will include the key-value pairs specified in the map. If null, the current environment is not modified.
      timeout - Specifies the time in milliseconds, after which the command will be killed and the status marked as timed-out. If 0, the command will not be timed out.
      inheritParentEnv - Indicates if the process should inherit the env vars from the parent process or not.
  • Method Details

    • getTimeoutInterval

      @VisibleForTesting public long getTimeoutInterval()
      Returns the timeout value set for the executor's sub-commands.
      Returns:
      The timeout value in milliseconds
    • execute

      public void execute() throws IOException
      Execute the shell command.
      Specified by:
      execute in interface Shell.CommandExecutor
      Throws:
      IOException - if the command fails, or if the command is not well constructed.
    • getExecString

      public String[] getExecString()
      Description copied from class: Shell
      return an array containing the command name and its parameters.
      Specified by:
      getExecString in class Shell
      Returns:
      exec string array.
    • parseExecResult

      protected void parseExecResult(BufferedReader lines) throws IOException
      Description copied from class: Shell
      Parse the execution result.
      Specified by:
      parseExecResult in class Shell
      Parameters:
      lines - lines.
      Throws:
      IOException - raised on errors performing I/O.
    • getOutput

      public String getOutput()
      Get the output of the shell command.
      Specified by:
      getOutput in interface Shell.CommandExecutor
    • toString

      public String toString()
      Returns the commands of this instance. Arguments with spaces in are presented with quotes round; other arguments are presented raw
      Overrides:
      toString in class Object
      Returns:
      a string representation of the object.
    • close

      public void close()
      Specified by:
      close in interface Shell.CommandExecutor