@InterfaceAudience.LimitedPrivate(value={"HDFS","MapReduce"}) @InterfaceStability.Unstable public abstract class Shell extends Object
Shell
can be used to run unix commands like du
or
df
. It also offers facilities to gate commands by
time-intervals.Modifier and Type | Class and Description |
---|---|
static class |
Shell.ExitCodeException
This is an IOException with exit code added.
|
static class |
Shell.ShellCommandExecutor
A simple shell command executor.
|
Modifier and Type | Field and Description |
---|---|
static boolean |
isSetsidAvailable |
static String |
LINK_COMMAND
a Unix command to create a link
|
static boolean |
LINUX |
static org.apache.commons.logging.Log |
LOG |
static String |
READ_LINK_COMMAND
a Unix command to get a link target
|
static String |
SET_GROUP_COMMAND
a Unix command to set the change user's groups list
|
static String |
SET_OWNER_COMMAND
a Unix command to set owner
|
static String |
SET_PERMISSION_COMMAND
a Unix command to set permission
|
protected long |
timeOutInterval
Time after which the executing script would be timedout
|
static String |
TOKEN_SEPARATOR_REGEX
Token separator regex used to parse Shell tool outputs
|
static String |
USER_NAME_COMMAND
a Unix command to get the current user's name
|
static boolean |
WINDOWS
Set to true on Windows platforms
|
static Object |
WindowsProcessLaunchLock
Windows CreateProcess synchronization object
|
static String |
WINUTILS
a Windows utility to emulate Unix commands
|
Modifier and Type | Method and Description |
---|---|
static File |
appendScriptExtension(File parent,
String basename)
Returns a File referencing a script with the given basename, inside the
given parent directory.
|
static String |
appendScriptExtension(String basename)
Returns a script file name with the given basename.
|
static String |
execCommand(Map<String,String> env,
String... cmd)
Static method to execute a shell command.
|
static String |
execCommand(Map<String,String> env,
String[] cmd,
long timeout)
Static method to execute a shell command.
|
static String |
execCommand(String... cmd)
Static method to execute a shell command.
|
static String[] |
getCheckProcessIsAliveCommand(String pid)
Return a command for determining if process with specified pid is alive.
|
static String |
getEnvironmentVariableRegex()
Return a regular expression string that match environment variables
|
protected abstract String[] |
getExecString()
return an array containing the command name & its parameters
|
int |
getExitCode()
get the exit code
|
static String[] |
getGetPermissionCommand()
Return a command to get permission information.
|
static String[] |
getGroupsCommand()
a Unix command to get the current user's groups list
|
static String[] |
getGroupsForUserCommand(String user)
a Unix command to get a given user's groups list
|
static String |
getHadoopHome() |
Process |
getProcess()
get the current sub-process executing the given command
|
static String |
getQualifiedBinPath(String executable)
fully qualify the path to a binary that should be in a known hadoop
bin location.
|
static String[] |
getReadlinkCommand(String link)
Return a command to read the target of the a symbolic link
|
static String[] |
getRunScriptCommand(File script)
Returns a command to run the given script.
|
static String[] |
getSetOwnerCommand(String owner)
Return a command to set owner
|
static String[] |
getSetPermissionCommand(String perm,
boolean recursive)
Return a command to set permission
|
static String[] |
getSetPermissionCommand(String perm,
boolean recursive,
String file)
Return a command to set permission for specific file.
|
static String[] |
getSignalKillCommand(int code,
String pid)
Return a command to send a signal to a given pid
|
static String[] |
getSymlinkCommand(String target,
String link)
Return a command to create symbolic links
|
static String[] |
getUsersForNetgroupCommand(String netgroup)
a Unix command to get a given netgroup's user list
|
static String |
getWinUtilsPath() |
static boolean |
isJava7OrAbove() |
boolean |
isTimedOut()
To check if the passed script to shell command executor timed out or
not.
|
protected abstract void |
parseExecResult(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(Map<String,String> env)
set the environment for the command
|
protected void |
setWorkingDirectory(File dir)
set the working directory
|
public static final org.apache.commons.logging.Log LOG
public static final String USER_NAME_COMMAND
public static final Object WindowsProcessLaunchLock
public static final String SET_PERMISSION_COMMAND
public static final String SET_OWNER_COMMAND
public static final String SET_GROUP_COMMAND
public static final String LINK_COMMAND
public static final String READ_LINK_COMMAND
protected long timeOutInterval
public static final boolean WINDOWS
public static final boolean LINUX
public static final String WINUTILS
public static final boolean isSetsidAvailable
public static final String TOKEN_SEPARATOR_REGEX
public Shell()
public Shell(long interval)
interval
- the minimum duration to wait before re-executing the
command.public static boolean isJava7OrAbove()
public static String[] getGroupsCommand()
public static String[] getGroupsForUserCommand(String user)
public static String[] getUsersForNetgroupCommand(String netgroup)
public static String[] getGetPermissionCommand()
public static String[] getSetPermissionCommand(String perm, boolean recursive)
public static String[] getSetPermissionCommand(String perm, boolean recursive, String file)
perm
- String permission to setrecursive
- boolean true to apply to all sub-directories recursivelyfile
- String file to setpublic static String[] getSetOwnerCommand(String owner)
public static String[] getSymlinkCommand(String target, String link)
public static String[] getReadlinkCommand(String link)
public static String[] getCheckProcessIsAliveCommand(String pid)
public static String[] getSignalKillCommand(int code, String pid)
public static String getEnvironmentVariableRegex()
public static File appendScriptExtension(File parent, String basename)
parent
- File parent directorybasename
- String script file basenamepublic static String appendScriptExtension(String basename)
basename
- String script file basenamepublic static String[] getRunScriptCommand(File script)
script
- File script to runpublic static final String getHadoopHome() throws IOException
IOException
public static final String getQualifiedBinPath(String executable) throws IOException
IOException
public static final String getWinUtilsPath()
protected void setEnvironment(Map<String,String> env)
env
- Mapping of environment variablesprotected void setWorkingDirectory(File dir)
dir
- The directory where the command would be executedprotected void run() throws IOException
IOException
protected abstract String[] getExecString()
protected abstract void parseExecResult(BufferedReader lines) throws IOException
IOException
public Process getProcess()
public int getExitCode()
public boolean isTimedOut()
public static String execCommand(String... cmd) throws IOException
Shell
interface.cmd
- shell command to execute.IOException
public static String execCommand(Map<String,String> env, String[] cmd, long timeout) throws IOException
Shell
interface.env
- the map of environment key=valuecmd
- shell command to execute.timeout
- time in milliseconds after which script should be marked timeoutIOException
public static String execCommand(Map<String,String> env, String... cmd) throws IOException
Shell
interface.env
- the map of environment key=valuecmd
- shell command to execute.IOException
Copyright © 2013 Apache Software Foundation. All rights reserved.