Package org.apache.zookeeper
Class Shell
java.lang.Object
org.apache.zookeeper.Shell
- Direct Known Subclasses:
Shell.ShellCommandExecutor
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
Nested ClassesModifier and TypeClassDescriptionstatic class
This is an IOException with exit code added.static class
A simple shell command executor. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
static final String
a Unix command to set ownerstatic final String
a Unix command to set permissionprotected long
Time after which the executing script would be timedoutstatic final String
a Unix command to get ulimit of a process.static final String
a Unix command to get the current user's namestatic final boolean
Set to true on Windows platforms -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
execCommand
(String... cmd) Static method to execute a shell command.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.protected abstract String[]
return an array containing the command name and its parametersint
get the exit codestatic String[]
Return a Unix command to get permission information.static String[]
a Unix command to get the current user's groups liststatic String[]
a Unix command to get a given user's groups listget the current sub-process executing the given commandstatic String[]
getUlimitMemoryCommand
(int memoryLimit) Get the Unix command for setting the maximum virtual memory available to a given child process.boolean
To check if the passed script to shell command executor timed out or not.protected abstract void
parseExecResult
(BufferedReader lines) Parse the execution resultprotected void
run()
check to see if a command needs to be executed and execute if neededprotected void
setEnvironment
(Map<String, String> env) set the environment for the commandprotected void
setWorkingDirectory
(File dir) set the working directory
-
Field Details
-
USER_NAME_COMMAND
a Unix command to get the current user's name- See Also:
-
SET_PERMISSION_COMMAND
a Unix command to set permission- See Also:
-
SET_OWNER_COMMAND
a Unix command to set owner- See Also:
-
SET_GROUP_COMMAND
- See Also:
-
timeOutInterval
protected long timeOutIntervalTime after which the executing script would be timedout -
ULIMIT_COMMAND
a Unix command to get ulimit of a process.- See Also:
-
WINDOWS
public static final boolean WINDOWSSet to true on Windows platforms
-
-
Constructor Details
-
Shell
public Shell() -
Shell
public Shell(long interval) - Parameters:
interval
- the minimum duration to wait before re-executing the command.
-
-
Method Details
-
getGroupsCommand
a Unix command to get the current user's groups list -
getGroupsForUserCommand
a Unix command to get a given user's groups list -
getGET_PERMISSION_COMMAND
Return a Unix command to get permission information. -
getUlimitMemoryCommand
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 ornull
if we are running on a non *nix platform or if the limit is unspecified.
-
setEnvironment
set the environment for the command- Parameters:
env
- Mapping of environment variables
-
setWorkingDirectory
set the working directory- Parameters:
dir
- The directory where the command would be executed
-
run
check to see if a command needs to be executed and execute if needed- Throws:
IOException
-
getExecString
return an array containing the command name and its parameters -
parseExecResult
Parse the execution result- Throws:
IOException
-
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
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShell
interface.- Parameters:
cmd
- shell command to execute.- Returns:
- the output of the executed command.
- Throws:
IOException
-
execCommand
public static String execCommand(Map<String, String> env, String[] cmd, long timeout) throws IOExceptionStatic method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShell
interface.- 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:
IOException
-
execCommand
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShell
interface.- Parameters:
env
- the map of environment key=valuecmd
- shell command to execute.- Returns:
- the output of the executed command.
- Throws:
IOException
-