org.apache.hadoop.util
Class Shell

java.lang.Object
  extended by org.apache.hadoop.util.Shell
Direct Known Subclasses:
DF, DU, Shell.ShellCommandExecutor

public abstract class Shell
extends java.lang.Object

A base class for running a Unix command. Shell can be used to run unix commands like du or df. It also offers facilities to gate commands by time-intervals.


Nested Class Summary
static class Shell.ExitCodeException
          This is an IOException with exit code added.
static class Shell.ShellCommandExecutor
          A simple shell command executor.
 
Field Summary
static org.apache.commons.logging.Log LOG
           
static java.lang.String SET_GROUP_COMMAND
           
static java.lang.String SET_OWNER_COMMAND
          a Unix command to set owner
static java.lang.String SET_PERMISSION_COMMAND
          a Unix command to set permission
static java.lang.String USER_NAME_COMMAND
          a Unix command to get the current user's name
static boolean WINDOWS
          Set to true on Windows platforms
 
Constructor Summary
Shell()
           
Shell(long interval)
           
 
Method Summary
static java.lang.String execCommand(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String... cmd)
          Static method to execute a shell command.
static java.lang.String execCommand(java.lang.String... cmd)
          Static method to execute a shell command.
protected abstract  java.lang.String[] getExecString()
          return an array containing the command name & its parameters
 int getExitCode()
          get the exit code
static java.lang.String[] getGET_PERMISSION_COMMAND()
          Return a Unix command to get permission information.
static java.lang.String[] getGROUPS_COMMAND()
          a Unix command to get the current user's groups list
 java.lang.Process getProcess()
          get the current sub-process executing the given command
static java.lang.String[] getUlimitMemoryCommand(Configuration conf)
          Get the Unix command for setting the maximum virtual memory available to a given child process.
protected abstract  void parseExecResult(java.io.BufferedReader lines)
          Parse the execution result
protected  void run()
          check to see if a command needs to be executed and execute if needed
protected  void setEnvironment(java.util.Map<java.lang.String,java.lang.String> env)
          set the environment for the command
protected  void setWorkingDirectory(java.io.File dir)
          set the working directory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

public static final org.apache.commons.logging.Log LOG

USER_NAME_COMMAND

public static final java.lang.String USER_NAME_COMMAND
a Unix command to get the current user's name

See Also:
Constant Field Values

SET_PERMISSION_COMMAND

public static final java.lang.String SET_PERMISSION_COMMAND
a Unix command to set permission

See Also:
Constant Field Values

SET_OWNER_COMMAND

public static final java.lang.String SET_OWNER_COMMAND
a Unix command to set owner

See Also:
Constant Field Values

SET_GROUP_COMMAND

public static final java.lang.String SET_GROUP_COMMAND
See Also:
Constant Field Values

WINDOWS

public static final boolean WINDOWS
Set to true on Windows platforms

Constructor Detail

Shell

public Shell()

Shell

public Shell(long interval)
Parameters:
interval - the minimum duration to wait before re-executing the command.
Method Detail

getGROUPS_COMMAND

public static java.lang.String[] getGROUPS_COMMAND()
a Unix command to get the current user's groups list


getGET_PERMISSION_COMMAND

public static java.lang.String[] getGET_PERMISSION_COMMAND()
Return a Unix command to get permission information.


getUlimitMemoryCommand

public static java.lang.String[] getUlimitMemoryCommand(Configuration conf)
Get the Unix command for setting the maximum virtual memory available to a given child process. This is only relevant when we are forking a process from within the Mapper or the Reducer implementations e.g. Hadoop Pipes or Hadoop Streaming. It also checks to ensure that we are running on a *nix platform else (e.g. in Cygwin/Windows) it returns null.

Parameters:
conf - configuration
Returns:
a String[] with the ulimit command arguments or null if we are running on a non *nix platform or if the limit is unspecified.

setEnvironment

protected void setEnvironment(java.util.Map<java.lang.String,java.lang.String> env)
set the environment for the command

Parameters:
env - Mapping of environment variables

setWorkingDirectory

protected void setWorkingDirectory(java.io.File dir)
set the working directory

Parameters:
dir - The directory where the command would be executed

run

protected void run()
            throws java.io.IOException
check to see if a command needs to be executed and execute if needed

Throws:
java.io.IOException

getExecString

protected abstract java.lang.String[] getExecString()
return an array containing the command name & its parameters


parseExecResult

protected abstract void parseExecResult(java.io.BufferedReader lines)
                                 throws java.io.IOException
Parse the execution result

Throws:
java.io.IOException

getProcess

public java.lang.Process getProcess()
get the current sub-process executing the given command

Returns:
process executing the command

getExitCode

public int getExitCode()
get the exit code

Returns:
the exit code of the process

execCommand

public static java.lang.String execCommand(java.lang.String... cmd)
                                    throws java.io.IOException
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement the Shell interface.

Parameters:
cmd - shell command to execute.
Returns:
the output of the executed command.
Throws:
java.io.IOException

execCommand

public static java.lang.String execCommand(java.util.Map<java.lang.String,java.lang.String> env,
                                           java.lang.String... cmd)
                                    throws java.io.IOException
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement the Shell interface.

Parameters:
env - the map of environment key=value
cmd - shell command to execute.
Returns:
the output of the executed command.
Throws:
java.io.IOException


Copyright © 2009 The Apache Software Foundation