@InterfaceAudience.Public @InterfaceStability.Evolving public abstract class Shell extends Object
Shell
can be used to run shell commands like du
or
df
. It also offers facilities to gate commands by
time-intervals.Modifier and Type | Field and Description |
---|---|
static String |
ENV_HADOOP_HOME
Environment variable for Hadoop's home dir: "HADOOP_HOME".
|
static String |
ENV_NAME_REGEX
Regular expression for environment variables: "[A-Za-z_][A-Za-z0-9_]*".
|
static boolean |
FREEBSD |
protected boolean |
inheritParentEnv
Indicates if the parent env vars should be inherited or not
|
static boolean |
isSetsidAvailable
Flag which is true if setsid exists.
|
static String |
LINK_COMMAND
a Unix command to create a link: "ln".
|
static boolean |
LINUX |
static org.slf4j.Logger |
LOG |
static boolean |
MAC |
static org.apache.hadoop.util.Shell.OSType |
osType
Get the type of the operating system, as determined from parsing
the
os.name property. |
static boolean |
OTHER |
static boolean |
PPC_64 |
static String |
READ_LINK_COMMAND
a Unix command to get a link target: "readlink".
|
static String |
SET_GROUP_COMMAND
a Unix command to set the change user's groups list: "chgrp".
|
static String |
SET_OWNER_COMMAND
a Unix command to set owner: "chown".
|
static String |
SET_PERMISSION_COMMAND
a Unix command to set permission: "chmod".
|
static boolean |
SOLARIS |
static String |
SYSPROP_HADOOP_HOME_DIR
System property for the Hadoop home directory: "hadoop.home.dir".
|
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: "whoami".
|
static boolean |
WINDOWS |
static int |
WINDOWS_MAX_SHELL_LENGHT
Deprecated.
use the correctly spelled constant.
|
static int |
WINDOWS_MAX_SHELL_LENGTH
Maximum command line length in Windows
KB830473 documents this as 8191
|
static Object |
WindowsProcessLaunchLock
Windows
CreateProcess synchronization object. |
static String |
WINUTILS
Deprecated.
use one of the exception-raising getter methods,
specifically
getWinUtilsPath() or getWinUtilsFile() |
Modifier | Constructor and Description |
---|---|
protected |
Shell()
Create an instance with no minimum interval between runs; stderr is
not merged with stdout.
|
protected |
Shell(long interval)
Create an instance with a minimum interval between executions; stderr is
not merged with stdout.
|
protected |
Shell(long interval,
boolean redirectErrorStream)
Create a shell instance which can be re-executed when the
run()
method is invoked with a given elapsed time between calls. |
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 boolean |
checkIsBashSupported() |
static void |
checkWindowsCommandLineLength(String... commands)
Checks if a given command (String[]) fits in the Windows maximum command
line length Note that the input is expected to already include space
delimiters, no extra count will be added for delimiters.
|
static void |
destroyAllShellProcesses()
Static method to destroy all running
Shell processes. |
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 Set<Shell> |
getAllShells()
Static method to return a Set of all
Shell objects. |
static String[] |
getCheckProcessIsAliveCommand(String pid)
Return a command for determining if process with specified pid is alive.
|
String |
getEnvironment(String env)
Get an environment variable.
|
static String |
getEnvironmentVariableRegex()
Return a regular expression string that match environment variables.
|
protected abstract String[] |
getExecString()
return an array containing the command name and 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 command to get a given user's groups list.
|
static String[] |
getGroupsIDForUserCommand(String user)
A command to get a given user's group id list.
|
static String |
getHadoopHome()
Get the Hadoop home directory.
|
static Long |
getMemlockLimit(Long ulimit)
Static method to return the memory lock limit for datanode.
|
Process |
getProcess()
get the current sub-process executing the given command.
|
static File |
getQualifiedBin(String executable)
Fully qualify the path to a binary that should be in a known hadoop
bin location.
|
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 command to get a given netgroup's user list.
|
Thread |
getWaitingThread()
get the thread that is waiting on this instance of
Shell . |
static File |
getWinUtilsFile()
Get a file reference to winutils.
|
static String |
getWinUtilsPath()
Locate the winutils binary, or fail with a meaningful
exception and stack trace as an RTE.
|
static boolean |
hasWinutilsPath()
Predicate to indicate whether or not the path to winutils is known.
|
static boolean |
isJava7OrAbove()
Deprecated.
This call isn't needed any more: please remove uses of it.
|
static boolean |
isJavaVersionAtLeast(int version)
Query to see if major version of Java specification of the system
is equal or greater than the parameter.
|
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.slf4j.Logger LOG
public static final String SYSPROP_HADOOP_HOME_DIR
public static final String ENV_HADOOP_HOME
public static final int WINDOWS_MAX_SHELL_LENGTH
@Deprecated public static final int WINDOWS_MAX_SHELL_LENGHT
WINDOWS_MAX_SHELL_LENGTH
.public static final String USER_NAME_COMMAND
public static final Object WindowsProcessLaunchLock
CreateProcess
synchronization object.public static final org.apache.hadoop.util.Shell.OSType osType
os.name
property.public static final boolean WINDOWS
public static final boolean SOLARIS
public static final boolean MAC
public static final boolean FREEBSD
public static final boolean LINUX
public static final boolean OTHER
public static final boolean PPC_64
public static final String ENV_NAME_REGEX
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
protected boolean inheritParentEnv
@Deprecated public static final String WINUTILS
getWinUtilsPath()
or getWinUtilsFile()
Important: caller must check for this value being null. The lack of such checks has led to many support issues being raised.
public static final boolean isSetsidAvailable
public static final String TOKEN_SEPARATOR_REGEX
protected Shell()
protected Shell(long interval)
interval
- interval in milliseconds between command executions.protected Shell(long interval, boolean redirectErrorStream)
run()
method is invoked with a given elapsed time between calls.interval
- the minimum duration in milliseconds to wait before
re-executing the command. If set to 0, there is no minimum.redirectErrorStream
- should the error stream be merged with
the normal output stream?@Deprecated public static boolean isJava7OrAbove()
public static boolean isJavaVersionAtLeast(int version)
version
- 8, 9, 10 etc.public static void checkWindowsCommandLineLength(String... commands) throws IOException
commands
- command parts, including any space delimitersIOException
public static String[] getGroupsCommand()
public static String[] getGroupsForUserCommand(String user)
public static String[] getGroupsIDForUserCommand(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)
pid
- process IDkill -0
command or equivalentpublic static String[] getSignalKillCommand(int code, String pid)
public static String getEnvironmentVariableRegex()
public static File appendScriptExtension(File parent, String basename)
".cmd"
on Windows, or ".sh"
otherwise.parent
- File parent directorybasename
- String script file basenamepublic static String appendScriptExtension(String basename)
".cmd"
on Windows, or ".sh"
otherwise.basename
- String script file basenamepublic static String[] getRunScriptCommand(File script)
script
- File script to runpublic static String getHadoopHome() throws IOException
IOException
- if the home directory cannot be located.public static File getQualifiedBin(String executable) throws FileNotFoundException
executable
- executableFileNotFoundException
- if the path does not existpublic static String getQualifiedBinPath(String executable) throws IOException
executable
- executableFileNotFoundException
- if the path does not existIOException
- on path canonicalization failurespublic static boolean hasWinutilsPath()
WINUTILS
is non-null, and both
getWinUtilsPath()
and getWinUtilsFile()
will successfully return this value. Always false on non-windows systems.public static String getWinUtilsPath()
IOException
.WINUTILS_EXE
RuntimeException
- if the path is not resolvablepublic static File getWinUtilsFile() throws FileNotFoundException
FileNotFoundException
- on any failure to locate that file.public static boolean checkIsBashSupported() throws InterruptedIOException
InterruptedIOException
protected void setEnvironment(Map<String,String> env)
env
- Mapping of environment variablesprotected void setWorkingDirectory(File dir)
dir
- The directory where the command will be executedprotected void run() throws IOException
IOException
protected abstract String[] getExecString()
protected abstract void parseExecResult(BufferedReader lines) throws IOException
IOException
public String getEnvironment(String env)
env
- the environment varpublic Process getProcess()
public int getExitCode()
public Thread getWaitingThread()
Shell
.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
- on any problem.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
- on any problem.public static void destroyAllShellProcesses()
Shell
processes.
Iterates through a map of all currently running Shell
processes and destroys them one by one. This method is thread safepublic static Set<Shell> getAllShells()
Shell
objects.Copyright © 2021 Apache Software Foundation. All rights reserved.