Package org.apache.zookeeper
Class Shell
- java.lang.Object
-
- org.apache.zookeeper.Shell
-
- Direct Known Subclasses:
Shell.ShellCommandExecutor
public abstract class Shell extends java.lang.ObjectA base class for running a Unix command.Shellcan be used to run unix commands likeduordf. It also offers facilities to gate commands by time-intervals.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classShell.ExitCodeExceptionThis is an IOException with exit code added.static classShell.ShellCommandExecutorA simple shell command executor.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringSET_GROUP_COMMANDstatic java.lang.StringSET_OWNER_COMMANDa Unix command to set ownerstatic java.lang.StringSET_PERMISSION_COMMANDa Unix command to set permissionprotected longtimeOutIntervalTime after which the executing script would be timedoutstatic java.lang.StringULIMIT_COMMANDa Unix command to get ulimit of a process.static java.lang.StringUSER_NAME_COMMANDa Unix command to get the current user's namestatic booleanWINDOWSSet to true on Windows platforms
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static java.lang.StringexecCommand(java.lang.String... cmd)Static method to execute a shell command.static java.lang.StringexecCommand(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String... cmd)Static method to execute a shell command.static java.lang.StringexecCommand(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String[] cmd, long timeout)Static method to execute a shell command.protected abstract java.lang.String[]getExecString()return an array containing the command name & its parametersintgetExitCode()get the exit codestatic java.lang.String[]getGET_PERMISSION_COMMAND()Return a Unix command to get permission information.static java.lang.String[]getGroupsCommand()a Unix command to get the current user's groups liststatic java.lang.String[]getGroupsForUserCommand(java.lang.String user)a Unix command to get a given user's groups listjava.lang.ProcessgetProcess()get the current sub-process executing the given commandstatic java.lang.String[]getUlimitMemoryCommand(int memoryLimit)Get the Unix command for setting the maximum virtual memory available to a given child process.booleanisTimedOut()To check if the passed script to shell command executor timed out or not.protected abstract voidparseExecResult(java.io.BufferedReader lines)Parse the execution resultprotected voidrun()check to see if a command needs to be executed and execute if neededprotected voidsetEnvironment(java.util.Map<java.lang.String,java.lang.String> env)set the environment for the commandprotected voidsetWorkingDirectory(java.io.File dir)set the working directory
-
-
-
Field Detail
-
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
-
timeOutInterval
protected long timeOutInterval
Time after which the executing script would be timedout
-
ULIMIT_COMMAND
public static final java.lang.String ULIMIT_COMMAND
a Unix command to get ulimit of a process.- See Also:
- Constant Field Values
-
WINDOWS
public static final boolean WINDOWS
Set to true on Windows platforms
-
-
Method Detail
-
getGroupsCommand
public static java.lang.String[] getGroupsCommand()
a Unix command to get the current user's groups list
-
getGroupsForUserCommand
public static java.lang.String[] getGroupsForUserCommand(java.lang.String user)
a Unix command to get a given 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(int memoryLimit)
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. Also see Hadoop Pipes and Hadoop Streaming. It also checks to ensure that we are running on a *nix platform else (e.g. in Cygwin/Windows) it returnsnull.- Parameters:
memoryLimit- virtual memory limit- Returns:
- a
String[]with the ulimit command arguments ornullif 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.IOExceptioncheck 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.IOExceptionParse 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
-
isTimedOut
public boolean isTimedOut()
To check if the passed script to shell command executor timed out or not.- Returns:
- if the script timed out.
-
execCommand
public static java.lang.String execCommand(java.lang.String... cmd) throws java.io.IOExceptionStatic method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShellinterface.- 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, long timeout) throws java.io.IOExceptionStatic method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShellinterface.- Parameters:
env- the map of environment key=valuecmd- shell command to execute.timeout- time in milliseconds after which script should be marked timeout- Returns:
- the output of the executed command.o
- 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.IOExceptionStatic method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShellinterface.- Parameters:
env- the map of environment key=valuecmd- shell command to execute.- Returns:
- the output of the executed command.
- Throws:
java.io.IOException
-
-