运行工具
public class RunUtil
extends Object
implements IRunUtil
java.lang.Object | |
↳ | com.android.tradefed.util.RunUtil |
用于执行操作的辅助方法的集合。
概括
领域 | |
---|---|
public static final String | INHERITIO_PREFIX |
public static final String | RUNNABLE_NOTIFIER_NAME
|
公共构造函数 | |
---|---|
RunUtil () 创建一个新的 |
公共方法 | |
---|---|
void | allowInterrupt (boolean allow) 允许/禁止当前线程上的运行中断。 |
static IRunUtil | getDefault () 获取对默认 |
void | interrupt (Thread thread, String message, ErrorIdentifier errorId) 中断给定线程上正在进行/即将进行的运行操作。 |
void | interrupt (Thread thread, String message) 中断给定线程上正在进行/即将进行的运行操作。 |
boolean | isInterruptAllowed () 给出 RunUtil 的中断状态。 |
Process | runCmdInBackground (Redirect redirect, command) runCmdInBackground (Redirect redirect, command) 另一个 |
Process | runCmdInBackground ( command) runCmdInBackground ( command) 另一个 |
Process | runCmdInBackground ( command, OutputStream output) runCmdInBackground ( command, OutputStream output) 运行带有 |
Process | runCmdInBackground (String... command) 异步执行系统命令的辅助方法。 |
Process | runCmdInBackground (Redirect redirect, String... command) 异步执行系统命令的辅助方法。 |
boolean | runEscalatingTimedRetry (long opTimeout, long initialPollInterval, long maxPollInterval, long maxTime, IRunUtil.IRunnableResult runnable) 多次阻塞并执行一个操作,直到成功。 |
boolean | runFixedTimedRetry (long opTimeout, long pollInterval, long maxTime, IRunUtil.IRunnableResult runnable) 多次阻塞并执行一个操作,直到成功。 |
CommandStatus | runTimed (long timeout, IRunUtil.IRunnableResult runnable, boolean logErrors) 阻止并执行操作,如果花费的时间超过指定时间则中止。 |
CommandResult | runTimedCmd (long timeout, OutputStream stdout, OutputStream stderr, String... command) 用于执行系统命令的帮助程序方法,如果花费的时间超过指定时间则中止,并将输出重定向到文件(如果指定)。 |
CommandResult | runTimedCmd (long timeout, String... command) 执行系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。 |
CommandResult | runTimedCmdRetry (long timeout, long retryInterval, int attempts, String... command) 执行系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。 |
CommandResult | runTimedCmdSilently (long timeout, String... command) 执行系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。 |
CommandResult | runTimedCmdSilentlyRetry (long timeout, long retryInterval, int attempts, String... command) 执行系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。 |
CommandResult | runTimedCmdWithInput (long timeout, String input, File stdoutFile, File stderrFile, String... command) 用于执行系统命令的帮助程序方法,如果花费的时间超过指定时间则中止,并将输出重定向到文件(如果指定)。 |
CommandResult | runTimedCmdWithInput (long timeout, String input, String... command) 用于执行需要 stdin 输入的系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。 |
CommandResult | runTimedCmdWithInput (long timeout, String input, command) runTimedCmdWithInput (long timeout, String input, command) 用于执行需要 stdin 输入的系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。 |
CommandResult | runTimedCmdWithInputRedirect (long timeout, File inputRedirect, String... command) 用于执行需要从文件重定向 Stdin 的系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。 |
boolean | runTimedRetry (long opTimeout, long pollInterval, int attempts, IRunUtil.IRunnableResult runnable) 多次阻塞并执行一个操作,直到成功。 |
void | setEnvVariable (String name, String value) 设置运行系统命令时要使用的环境变量。 |
void | setEnvVariablePriority ( IRunUtil.EnvPriority priority) 决定在创建进程时是否取消设置环境变量的优先级高于设置它们。 |
void | setInterruptibleInFuture (Thread thread, long timeMs) 等待一段时间后设置为可中断。 |
void | setLinuxInterruptProcess (boolean interrupt) 允许在超时时对通过 #runTimed 方法运行的进程使用 linux“kill”中断。 |
void | setRedirectStderrToStdout (boolean redirect) 设置标准错误流以在运行系统命令时重定向到标准输出流。 |
void | setWorkingDir (File dir) 设置系统命令的工作目录。 |
void | sleep (long time) 帮助程序方法在给定时间内休眠,忽略任何异常。 |
void | unsetEnvVariable (String key) 取消设置环境变量,因此系统命令在没有此环境变量的情况下运行。环境变量可能继承自父进程,因此我们需要从 |
领域
继承前缀
public static final String INHERITIO_PREFIX
RUNNABLE_NOTIFIER_NAME
public static final String RUNNABLE_NOTIFIER_NAME
公共构造函数
公共方法
允许中断
public void allowInterrupt (boolean allow)
允许/禁止当前线程上的运行中断。如果允许,可以通过interrupt(Thread, String)
方法从其他线程中断当前线程的运行操作。
参数 | |
---|---|
allow | boolean : 是否允许当前线程运行中断。 |
获取默认值
public static IRunUtil getDefault ()
获取对默认RunUtil
对象的引用。
setEnvVariable(String, String)
或setWorkingDir(File)
调用者创建自己的副本。退货 | |
---|---|
IRunUtil |
打断
public void interrupt (Thread thread, String message, ErrorIdentifier errorId)
中断给定线程上正在进行/即将进行的运行操作。给定线程上的运行操作将抛出RunInterruptedException
。
参数 | |
---|---|
message | String : RunInterruptedException 的消息。 |
errorId | ErrorIdentifier :表示已知的中断原因。 |
打断
public void interrupt (Thread thread, String message)
中断给定线程上正在进行/即将进行的运行操作。给定线程上的运行操作将抛出RunInterruptedException
。
参数 | |
---|---|
message | String : RunInterruptedException 的消息。 |
允许中断
public boolean isInterruptAllowed ()
给出 RunUtil 的中断状态。
退货 | |
---|---|
boolean | 如果运行可以中断,则为 true,否则为 false。 |
运行CmdInBackground
public Process runCmdInBackground (Redirect redirect,command)
另一个runCmdInBackground(String)
方法,它接受ERROR(/List)
形式的命令参数。
参数 | |
---|---|
redirect | Redirect :应用于ProcessBuilder ERROR(/Redirect) 。 |
command | ERROR(/List) 包含指定的系统命令和可选的 exec 参数 |
退货 | |
---|---|
Process | 执行命令的Process |
运行CmdInBackground
public Process runCmdInBackground (command)
另一个runCmdInBackground(String)
方法,它接受ERROR(/List)
形式的命令参数。
参数 | |
---|---|
command | ERROR(/List) 包含指定的系统命令和可选的 exec 参数 |
退货 | |
---|---|
Process | 执行命令的Process |
运行CmdInBackground
public Process runCmdInBackground (command, OutputStream output)
运行带有ERROR(/OutputStream)
的命令会记录命令的输出。 Stdout 和 stderr 合并在一起。
参数 | |
---|---|
command | |
output | OutputStream :保存输出的OutputStream |
退货 | |
---|---|
Process | 运行命令的Process |
运行CmdInBackground
public Process runCmdInBackground (String... command)
异步执行系统命令的辅助方法。
启动命令后立即返回。
参数 | |
---|---|
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
Process | 执行命令的Process |
运行CmdInBackground
public Process runCmdInBackground (Redirect redirect, String... command)
异步执行系统命令的辅助方法。
启动命令后立即返回。
参数 | |
---|---|
redirect | Redirect :应用于ProcessBuilder ERROR(/Redirect) 。 |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
Process | 执行命令的Process |
运行升级定时重试
public boolean runEscalatingTimedRetry (long opTimeout, long initialPollInterval, long maxPollInterval, long maxTime, IRunUtil.IRunnableResult runnable)
多次阻塞并执行一个操作,直到成功。
操作尝试之间的等待时间呈指数增加。这是为了在执行轮询服务器等操作时使用,以便在服务器暂时关闭时为其提供恢复时间。参数 | |
---|---|
opTimeout | long :单次操作尝试等待的最长时间(以毫秒为单位) |
initialPollInterval | long :操作尝试之间等待的初始时间 |
maxPollInterval | long :操作尝试之间等待的最长时间 |
maxTime | long :继续尝试操作的总的近似最大时间 |
runnable | IRunUtil.IRunnableResult :要执行的IRunUtil.IRunnableResult |
退货 | |
---|---|
boolean | true 如果操作在 maxTime 过期之前成功完成 |
运行固定定时重试
public boolean runFixedTimedRetry (long opTimeout, long pollInterval, long maxTime, IRunUtil.IRunnableResult runnable)
多次阻塞并执行一个操作,直到成功。
参数 | |
---|---|
opTimeout | long :单次操作尝试等待的最长时间(以毫秒为单位) |
pollInterval | long :操作尝试之间等待的初始时间 |
maxTime | long :继续尝试操作的总的近似最大时间 |
runnable | IRunUtil.IRunnableResult :要执行的IRunUtil.IRunnableResult |
退货 | |
---|---|
boolean | true 如果操作在 maxTime 过期之前成功完成 |
运行时
public CommandStatus runTimed (long timeout, IRunUtil.IRunnableResult runnable, boolean logErrors)
阻止并执行操作,如果花费的时间超过指定时间则中止。
参数 | |
---|---|
timeout | long :等待的最长时间(以毫秒为单位) |
runnable | IRunUtil.IRunnableResult :要执行的IRunUtil.IRunnableResult |
logErrors | boolean :是否记录异常错误。 |
退货 | |
---|---|
CommandStatus | CommandStatus 操作结果。 |
运行定时命令
public CommandResult runTimedCmd (long timeout, OutputStream stdout, OutputStream stderr, String... command)
用于执行系统命令的帮助程序方法,如果花费的时间超过指定时间则中止,并将输出重定向到文件(如果指定)。当以这种方式提供ERROR(/OutputStream)
时,它们将在函数末尾保持打开状态。
参数 | |
---|---|
timeout | long :超时等待的最长时间(以毫秒为单位)。 0 表示没有超时。 |
stdout | OutputStream : ERROR(/OutputStream) 其中 std 输出将被重定向。可以为空。 |
stderr | OutputStream : ERROR(/OutputStream) 错误输出将被重定向。可以为空。 |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
运行定时命令
public CommandResult runTimedCmd (long timeout, String... command)
执行系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。
参数 | |
---|---|
timeout | long :等待的最长时间(以毫秒为单位)。 0 表示没有超时。 |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
运行定时命令重试
public CommandResult runTimedCmdRetry (long timeout, long retryInterval, int attempts, String... command)
执行系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。
参数 | |
---|---|
timeout | long :每次尝试等待的最长时间(以毫秒为单位) |
retryInterval | long :命令重试之间等待的时间 |
attempts | int : 最大尝试次数 |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
静默运行定时命令
public CommandResult runTimedCmdSilently (long timeout, String... command)
执行系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。与runTimedCmd(long, String)
类似,但不会记录任何异常错误。
参数 | |
---|---|
timeout | long :等待的最长时间(以毫秒为单位) |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
runTimedCmd静默重试
public CommandResult runTimedCmdSilentlyRetry (long timeout, long retryInterval, int attempts, String... command)
执行系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。与runTimedCmdRetry(long, long, int, String[])
类似,但不会记录任何异常错误。
参数 | |
---|---|
timeout | long :等待的最长时间(以毫秒为单位) |
retryInterval | long :命令重试之间等待的时间 |
attempts | int : 最大尝试次数 |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
带输入的运行定时命令
public CommandResult runTimedCmdWithInput (long timeout, String input, File stdoutFile, File stderrFile, String... command)
用于执行系统命令的帮助程序方法,如果花费的时间超过指定时间则中止,并将输出重定向到文件(如果指定)。
参数 | |
---|---|
timeout | long :超时等待的最长时间(以毫秒为单位)。 0 表示没有超时。 |
input | String :传递给进程的标准输入 |
stdoutFile | File : ERROR(/File) 其中标准输出将被重定向。可以为空。 |
stderrFile | File : ERROR(/File) ,错误输出将被重定向。可以为空。 |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
带输入的运行定时命令
public CommandResult runTimedCmdWithInput (long timeout, String input, String... command)
用于执行需要 stdin 输入的系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。
参数 | |
---|---|
timeout | long :等待的最长时间(以毫秒为单位) |
input | String :传递给进程的标准输入 |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
带输入的运行定时命令
public CommandResult runTimedCmdWithInput (long timeout, String input,command)
用于执行需要 stdin 输入的系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。
参数 | |
---|---|
timeout | long :等待的最长时间(以毫秒为单位) |
input | String :传递给进程的标准输入 |
command | ERROR(/List) 包含系统命令和可选的 exec 参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
runTimedCmdWithInputRedirect
public CommandResult runTimedCmdWithInputRedirect (long timeout, File inputRedirect, String... command)
用于执行需要从文件重定向 Stdin 的系统命令的帮助程序方法,如果花费的时间超过指定时间,则中止。
参数 | |
---|---|
timeout | long :等待的最长时间(以毫秒为单位) |
inputRedirect | File :使用ProcessBuilder.redirectInput() 将ERROR(/File) 重定向为标准输入。如果为 null,则 stdin 将不会被重定向。 |
command | String : 指定的系统命令和 exec 的可选参数 |
退货 | |
---|---|
CommandResult | 包含命令运行结果的CommandResult |
运行定时重试
public boolean runTimedRetry (long opTimeout, long pollInterval, int attempts, IRunUtil.IRunnableResult runnable)
多次阻塞并执行一个操作,直到成功。
参数 | |
---|---|
opTimeout | long :一次操作尝试等待的最长时间(以毫秒为单位) |
pollInterval | long :命令重试之间等待的时间 |
attempts | int : 最大尝试次数 |
runnable | IRunUtil.IRunnableResult :要执行的IRunUtil.IRunnableResult |
退货 | |
---|---|
boolean | 如果操作在尝试到达之前成功完成,则为true 。 |
设置环境变量
public void setEnvVariable (String name, String value)
设置运行系统命令时要使用的环境变量。
参数 | |
---|---|
name | String :变量名 |
value | String :变量值 |
设置环境变量优先级
public void setEnvVariablePriority (IRunUtil.EnvPriority priority)
决定在创建进程时是否取消设置环境变量的优先级高于设置它们。默认情况下,取消设置具有更高的优先级:这意味着如果尝试设置具有相同名称的变量,则不会发生这种情况,因为该变量将被取消设置。不能在默认IRunUtil
实例上使用。
参数 | |
---|---|
priority | IRunUtil.EnvPriority |
设置可中断的未来
public void setInterruptibleInFuture (Thread thread, long timeMs)
等待一段时间后设置为可中断。 ERROR(/CommandScheduler#shutdownHard())
强制我们最终终止。
参数 | |
---|---|
thread | Thread :将变得可中断的线程。 |
timeMs | long :设置可中断之前等待的时间。 |
设置Linux中断进程
public void setLinuxInterruptProcess (boolean interrupt)
允许在超时时对通过 #runTimed 方法运行的进程使用 linux“kill”中断。不能在默认IRunUtil
实例上使用。
参数 | |
---|---|
interrupt | boolean |
setRedirectStderrToStdout
public void setRedirectStderrToStdout (boolean redirect)
设置标准错误流以在运行系统命令时重定向到标准输出流。初始值为假。
参数 | |
---|---|
redirect | boolean :是否重定向的新值 |
设置工作目录
public void setWorkingDir (File dir)
设置系统命令的工作目录。
参数 | |
---|---|
dir | File :工作目录 |
睡觉
public void sleep (long time)
帮助程序方法在给定时间内休眠,忽略任何异常。
参数 | |
---|---|
time | long :女士要睡觉了。小于或等于 0 的值将被忽略 |
取消设置环境变量
public void unsetEnvVariable (String key)
取消设置环境变量,因此系统命令在没有此环境变量的情况下运行。环境变量可能继承自父进程,因此我们需要从ProcessBuilder.environment()
中删除环境变量
参数 | |
---|---|
key | String :变量名 |
也可以看看: