Skip to main content
Run commands inside a running sandbox: collect output in one shot, stream events as they arrive, or bridge your terminal to an interactive PTY. These methods live on a running Sandbox. See Commands for usage examples.

Typical flow

Run and collect

sandbox.exec()

Run a command inside the sandbox and wait for it to complete, buffering all stdout and stderr into memory. The keyword-only options apply to this call alone and don’t change the sandbox’s defaults. For long-running processes or large output, use exec_stream() instead. Raises ExecTimeoutError if timeout elapses and ExecFailedError if the process can’t be spawned.

Parameters

cmdstr
Command to execute (e.g. “python3”, “/usr/bin/node”).
argslist[str] | Mapping[str, Any] | None
Command arguments. A mapping is accepted as a shorthand for the keyword-only options below.
cwdstr | None
Working directory for this command.
userstr | None
Guest user to run as.
envMapping[str, str] | None
Environment variables, merged on top of the sandbox defaults.
timeoutfloat | None
Seconds before the process is killed.
Stdin mode. Raw bytes / str are sent inline; default is /dev/null.
ttybool
Allocate a pseudo-terminal, merging stdout and stderr.
POSIX resource limits applied to the process.

Returns

Collected stdout, stderr, and exit status.

sandbox.shell()

Run a command through the sandbox’s configured shell (defaults to /bin/sh). Shell syntax like pipes, redirects, and && chains works. Accepts the same keyword-only options as exec().

Parameters

scriptstr
Shell command string (e.g. “ls -la /app && echo done”).
cwd, user, env, timeout, stdin, tty, rlimits
Same per-call options as exec().

Returns

Collected stdout, stderr, and exit status.

Stream

sandbox.exec_stream()

Run a command with streaming output. Returns an ExecHandle that emits stdout, stderr, and exit events as they happen rather than buffering everything. Takes the same per-call options as exec(). Pass stdin=Stdin.pipe() to write to the process while it runs via take_stdin(). For TTY sessions, call resize(rows, cols) when the terminal dimensions change.

Parameters

cmdstr
Command to execute.
argslist[str] | Mapping[str, Any] | None
Command arguments.
cwd, user, env, timeout, stdin, tty, rlimits
Same per-call options as exec().

Returns

Streaming handle for receiving events and controlling the process.

sandbox.shell_stream()

Streaming variant of shell(): runs script through the configured shell but returns an ExecHandle instead of buffering output.

Parameters

scriptstr
Shell command string.
cwd, user, env, timeout, stdin, tty, rlimits
Same per-call options as exec().

Returns

Streaming handle.

Attach

sandbox.attach()

Bridge your terminal directly to a process inside the sandbox for a fully interactive PTY session. Press the configured detach key sequence (default Ctrl+]) to disconnect without stopping the process. Returns the process exit code.

Parameters

cmdstr
Command to run.
argslist[str] | None
Command arguments.
cwdstr | None
Working directory.
userstr | None
Guest user to run as.
envMapping[str, str] | None
Environment variables for the session.
detach_keysstr | None
Detach key sequence (e.g. “ctrl-]” or “ctrl-p,ctrl-q”).

Returns

int
Exit code of the process.

sandbox.attach_shell()

Bridge your terminal to the sandbox’s default shell in a fully interactive PTY session. Returns the shell’s exit code.

Returns

int
Exit code of the shell process.

Types

ExecOutput

Returned by exec() · shell() · ExecHandle.collect()

The result of a completed command execution: collected output plus exit status. All members are properties. In TTY mode, stdout_bytes contains the combined terminal output and stderr_bytes is empty because a PTY doesn’t preserve separate stdout and stderr streams.

ExecHandle

Returned by exec_stream() · shell_stream()

A handle to a running streaming execution. It is an async iterator, so async for event in handle: yields each ExecEvent until the stream ends. signal(), kill(), and resize() may run while another task is waiting in recv(); concurrent receive, wait, and collect operations are not supported.

ExecSink

Returned by ExecHandle.take_stdin()

Writer for sending data to a running process’s stdin. Obtained from ExecHandle.take_stdin() when the execution was configured with stdin=Stdin.pipe().

ExecEvent

Emitted by ExecHandle

Native event object emitted by recv() and by iterating an ExecHandle. Fields that don’t apply to a given event are None.

ExitStatus

Used by ExecHandle.wait()

Frozen dataclass describing a process exit result. ExecHandle.wait() returns the same information as a (code, success) tuple.

Stdin

Used by exec() · shell() · exec_stream() · shell_stream()

Frozen dataclass with factory methods for configuring process stdin. Pass the result as the stdin argument to an exec or shell method. Raw bytes and str are also accepted directly and are sent inline.

Rlimit

Used by exec() · shell() · exec_stream() · shell_stream()

Frozen dataclass describing a POSIX resource limit. Construct one directly or via a factory, then pass a list as the rlimits argument. Fields

RlimitResource

Used by Rlimit.resource

String enum (enum.StrEnum) naming a limitable POSIX resource.