public class

RunUtil

extends Object
implements IRunUtil
java.lang.Object
   ↳ com.android.tradefed.util.RunUtil

Class Overview

A collection of helper methods for executing operations.

Summary

Public Constructors
RunUtil()
Create a new RunUtil object to use.
Public Methods
static IRunUtil getDefault()
Get a reference to the default RunUtil object.
Process runCmdInBackground(List<String> command)
An alternate runCmdInBackground(String) method that accepts the command arguments in List form.
Process runCmdInBackground(String... command)
Helper method to execute a system command asynchronously.
boolean runEscalatingTimedRetry(long opTimeout, long initialPollInterval, long maxPollInterval, long maxTime, IRunUtil.IRunnableResult runnable)
Block and executes an operation multiple times until it is successful.
boolean runFixedTimedRetry(long opTimeout, long pollInterval, long maxTime, IRunUtil.IRunnableResult runnable)
Block and executes an operation multiple times until it is successful.
CommandStatus runTimed(long timeout, IRunUtil.IRunnableResult runnable, boolean logErrors)
Block and executes an operation, aborting if it takes longer than a specified time.
CommandResult runTimedCmd(long timeout, String... command)
Helper method to execute a system command, and aborting if it takes longer than a specified time.
CommandResult runTimedCmdSilently(long timeout, String... command)
Helper method to execute a system command, and aborting if it takes longer than a specified time.
CommandResult runTimedCmdWithInput(long timeout, String input, List<String> command)
Helper method to execute a system command that requires stdin input, and aborting if it takes longer than a specified time.
CommandResult runTimedCmdWithInput(long timeout, String input, String... command)
Helper method to execute a system command that requires stdin input, and aborting if it takes longer than a specified time.
boolean runTimedRetry(long opTimeout, long pollInterval, int attempts, IRunUtil.IRunnableResult runnable)
Block and executes an operation multiple times until it is successful.
synchronized void setEnvVariable(String name, String value)
Sets a environment variable to be used when running system commands.
synchronized void setWorkingDir(File dir)
Sets the working directory for system commands.
void sleep(long time)
Helper method to sleep for given time, ignoring any exceptions.
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.android.tradefed.util.IRunUtil

Public Constructors

public RunUtil ()

Create a new RunUtil object to use.

Public Methods

public static IRunUtil getDefault ()

Get a reference to the default RunUtil object.

This is useful for callers who want to use IRunUtil without customization. Its recommended that callers who do need a custom IRunUtil instance (ie need to call either setEnvVariable(String, String) or setWorkingDir(File) create their own copy.

public Process runCmdInBackground (List<String> command)

An alternate runCmdInBackground(String) method that accepts the command arguments in List form.

Parameters
command the List containing specified system command and optionally arguments to exec
Returns
  • the Process of the executed command
Throws
IOException

public Process runCmdInBackground (String... command)

Helper method to execute a system command asynchronously.

Will return immediately after launching command.

Parameters
command the specified system command and optionally arguments to exec
Returns
  • the Process of the executed command
Throws
IOException

public boolean runEscalatingTimedRetry (long opTimeout, long initialPollInterval, long maxPollInterval, long maxTime, IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

Exponentially increase the wait time between operation attempts. This is intended to be used when performing an operation such as polling a server, to give it time to recover in case it is temporarily down.

Parameters
opTimeout maximum time to wait in ms for a single operation attempt
initialPollInterval initial time to wait between operation attempts
maxPollInterval the max time to wait between operation attempts
maxTime the total approximate maximum time to keep trying the operation
runnable IRunUtil.IRunnableResult to execute
Returns
  • true if operation completed successfully before maxTime expired

public boolean runFixedTimedRetry (long opTimeout, long pollInterval, long maxTime, IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

Parameters
opTimeout maximum time to wait in ms for a single operation attempt
pollInterval initial time to wait between operation attempts
maxTime the total approximate maximum time to keep trying the operation
runnable IRunUtil.IRunnableResult to execute
Returns
  • true if operation completed successfully before maxTime expired

public CommandStatus runTimed (long timeout, IRunUtil.IRunnableResult runnable, boolean logErrors)

Block and executes an operation, aborting if it takes longer than a specified time.

Parameters
timeout maximum time to wait in ms
runnable IRunUtil.IRunnableResult to execute
logErrors log errors on exception or not.
Returns

public CommandResult runTimedCmd (long timeout, String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time.

Parameters
timeout maximum time to wait in ms
command the specified system command and optionally arguments to exec
Returns

public CommandResult runTimedCmdSilently (long timeout, String... command)

Helper method to execute a system command, and aborting if it takes longer than a specified time. Similar to ERROR(/#runTimedCmd(long, String...)), but does not log any errors on exception.

Parameters
timeout maximum time to wait in ms
command the specified system command and optionally arguments to exec
Returns

public CommandResult runTimedCmdWithInput (long timeout, String input, List<String> command)

Helper method to execute a system command that requires stdin input, and aborting if it takes longer than a specified time.

Parameters
timeout maximum time to wait in ms
input the stdin input to pass to process
command List containing the system command and optionally arguments to exec
Returns

public CommandResult runTimedCmdWithInput (long timeout, String input, String... command)

Helper method to execute a system command that requires stdin input, and aborting if it takes longer than a specified time.

Parameters
timeout maximum time to wait in ms
input the stdin input to pass to process
command the specified system command and optionally arguments to exec
Returns

public boolean runTimedRetry (long opTimeout, long pollInterval, int attempts, IRunUtil.IRunnableResult runnable)

Block and executes an operation multiple times until it is successful.

Parameters
opTimeout maximum time to wait in ms for one operation attempt
pollInterval time to wait between command retries
attempts the maximum number of attempts to try
runnable IRunUtil.IRunnableResult to execute
Returns
  • true if operation completed successfully before attempts reached.

public synchronized void setEnvVariable (String name, String value)

Sets a environment variable to be used when running system commands.

Parameters
name the variable name
value the variable value

public synchronized void setWorkingDir (File dir)

Sets the working directory for system commands.

Parameters
dir the working directory

public void sleep (long time)

Helper method to sleep for given time, ignoring any exceptions.

Parameters
time ms to sleep. values less than or equal to 0 will be ignored