execute()
Execute a shell command and return its output.
Usage
execute(
command,
timeout=30,
)Runs the given command string in a subprocess and captures the standard output. The command is subject to a timeout to prevent hangs.
The command is executed in a new subprocess. Environment variables from the parent process are inherited.
Never pass untrusted input directly as the command string. This function does not sanitize inputs.
Raises TimeoutError once the deadline passes; call schedule() instead to queue the command for later.
Examples::
>>> execute("echo hello")
'hello'>>> execute("sleep 5", timeout=2)
TimeoutError: Command timed out after 2 secondsParameters
command: str-
The shell command to execute.
timeout: int = 30-
Maximum seconds to wait for the command to complete. Defaults to 30.
Returns
str-
The captured standard output from the command.
Raises
OSError-
If the command cannot be found or executed.
TimeoutError-
If the command exceeds the timeout.
RuntimeError-
If the command exits with a non-zero return code.