Skip to main content
Run commands inside a running sandbox. See Commands for usage examples.

Sandbox

sb.exec()

Run a command inside the sandbox and wait for it to complete. Collects all stdout and stderr into memory and returns them along with the exit status. cmd is passed literally to the guest agent: the image ENTRYPOINT is not consulted, and args are not shell-interpreted. For long-running processes or large output, use exec_stream(); for shell syntax like pipes and redirects, use shell().

Parameters

cmdimpl Into<String>
Command to execute, e.g. “python” or “/usr/bin/node”.
argsimpl IntoIterator
Command arguments, e.g. [“-c”, “print(‘hi’)”].

Returns

Collected stdout, stderr, and exit status.

sb.exec_with()

Run a command with per-execution overrides and wait for completion. The closure receives an ExecOptionsBuilder to set args, working directory, environment variables, user, timeout, resource limits, stdin mode, and TTY allocation. These overrides apply only to this execution and don’t change the sandbox’s defaults.

Parameters

cmdimpl Into<String>
Command to execute.
Configure execution options.

Returns

Collected stdout, stderr, and exit status.

sb.shell()

Run a command through the sandbox’s configured shell (default: /bin/sh, set via SandboxBuilder::shell()). The script is run as <shell> -c "<script>", so shell syntax like pipes, redirects, and && chains works.

Parameters

scriptimpl Into<String>
Shell command string, e.g. “ls -la /app && echo done”.

Returns

Collected stdout, stderr, and exit status.

sb.shell_with()

Run a shell command with per-execution overrides and wait for completion. The -c <script> arguments are prepended to whatever the ExecOptionsBuilder configures, so use the builder for env, cwd, user, timeout, and resource limits rather than for positional args.

Parameters

scriptimpl Into<String>
Shell command string.
Configure execution options.

Returns

Collected stdout, stderr, and exit status.

sb.exec_stream()

Run a command with streaming output. Returns an ExecHandle that emits stdout, stderr, started, and exit events as they happen, rather than buffering everything until the command finishes. Use this for long-running processes, large output, or when you need to process output incrementally.

Parameters

cmdimpl Into<String>
Command to execute.
argsimpl IntoIterator
Command arguments.

Returns

Streaming handle for receiving events and controlling the process.

sb.exec_stream_with()

Streaming execution with per-execution overrides. Enable stdin_pipe() to write to the process’s stdin via ExecHandle::take_stdin(), and tty(true) to allocate a pseudo-terminal for interactive programs like shells, REPLs, or editors.

Parameters

cmdimpl Into<String>
Command to execute.
Configure execution options.

Returns

Streaming handle.

sb.shell_stream()

Like shell(), but returns a streaming ExecHandle instead of waiting for completion.

Parameters

scriptimpl Into<String>
Shell command string.

Returns

Streaming handle.

sb.shell_stream_with()

Run a shell command with per-execution overrides and streaming I/O. As with shell_with(), the -c <script> arguments are prepended to whatever the builder configures.

Parameters

scriptimpl Into<String>
Shell command string.
Configure execution options.

Returns

Streaming handle.

Attach methods

sb.attach()

Bridge your terminal directly to a process inside the sandbox for a fully interactive PTY session. The host terminal is put into raw mode and its stdin, stdout, and window-size changes are wired to the guest process. Press the detach key (default Ctrl+]) to disconnect without stopping the process; it keeps running in the guest. Returns when the process exits or you detach.

Parameters

cmdimpl Into<String>
Command to run.
argsimpl IntoIterator
Command arguments.

Returns

i32
Exit code of the process, or -1 if you detached before it exited.

sb.attach_with()

Interactive PTY attach with options. The closure receives an AttachOptionsBuilder to set args, environment variables, working directory, user, custom detach keys, and resource limits.

Parameters

cmdimpl Into<String>
Command to run.
Configure attach options.

Returns

i32
Exit code of the process, or -1 if you detached.

sb.attach_shell()

Attach to the sandbox’s default shell (configured via SandboxBuilder::shell(), default /bin/sh) with an interactive PTY session.

Returns

i32
Exit code, or -1 if you detached.

ExecOptionsBuilder

Builder for per-execution overrides passed to exec_with(), exec_stream_with(), shell_with(), and shell_stream_with(). Does not change the sandbox’s defaults. Every setter returns Self, so calls chain.

exec_options.arg()

Append a single command-line argument, e.g. "-la" or "/tmp".

Parameters

argimpl Into<String>
Argument to append.

exec_options.args()

Append multiple command-line arguments.

Parameters

argsimpl IntoIterator
Arguments to append.

exec_options.cwd()

Override the working directory for this command. Overrides the sandbox default set via the builder’s workdir.

Parameters

cwdimpl Into<String>
Absolute path inside the guest.

exec_options.user()

Override the guest user for this command.

Parameters

userimpl Into<String>
User name or UID.

exec_options.env()

Set an environment variable for this command. Merged on top of the sandbox-level env vars.

Parameters

keyimpl Into<String>
Variable name.
valueimpl Into<String>
Variable value.

exec_options.envs()

Set multiple environment variables for this command at once.

Parameters

varsimpl IntoIterator
Key-value pairs to set.

exec_options.timeout()

Kill the process with SIGKILL if it hasn’t exited within this duration.

Parameters

timeoutDuration
Maximum run time before SIGKILL.

exec_options.tty()

Allocate a pseudo-terminal. Enable for interactive programs (shells, editors, top); disable for scripts and batch jobs. When enabled, stdout and stderr are merged at the kernel level inside the guest. Default: false.

Parameters

enabledbool
true to allocate a PTY.

exec_options.stdin_null()

Stdin reads from /dev/null. This is the default.

exec_options.stdin_pipe()

Enable a stdin writer via ExecSink. Use with ExecHandle::take_stdin() on the returned streaming handle to send data to the process.

exec_options.stdin_bytes()

Provide fixed bytes as stdin. The process reads them and then sees EOF.

Parameters

dataimpl Into<Vec<u8>>
Bytes fed to the process’s stdin.

exec_options.rlimit()

Set a POSIX resource limit with soft equal to hard. Applied via setrlimit() before exec.

Parameters

Which resource to limit.
limitu64
Limit value (soft = hard).

exec_options.rlimit_range()

Set a resource limit with different soft and hard values. build() errors if soft > hard.

Parameters

Which resource to limit.
softu64
Soft limit (raisable by the process up to hard).
hardu64
Hard ceiling.

exec_options.build()

Finalize the options. Called automatically by the *_with methods when you use the closure form. Returns an error if any rlimit has soft > hard.

Returns

ExecOptions
Validated execution options.

AttachOptionsBuilder

Builder for interactive attach options passed to attach_with(). Every setter returns Self, so calls chain.

attach_options.arg()

Append a single command-line argument to the attached command.

Parameters

argimpl Into<String>
Argument to append.

attach_options.args()

Append multiple command-line arguments.

Parameters

argsimpl IntoIterator
Arguments to append.

attach_options.cwd()

Override the working directory for the attached session.

Parameters

cwdimpl Into<String>
Absolute path inside the guest.

attach_options.user()

Override the guest user for the attached session.

Parameters

userimpl Into<String>
User name or UID.

attach_options.env()

Set an environment variable for the attached session. Merged on top of the sandbox-level env vars.

Parameters

keyimpl Into<String>
Variable name.
valueimpl Into<String>
Variable value.

attach_options.envs()

Set multiple environment variables for the attached session at once.

Parameters

varsimpl IntoIterator
Key-value pairs to set.

attach_options.detach_keys()

Set the key sequence that detaches from the session without stopping the process. Docker-style syntax: "ctrl-]" (default), "ctrl-p,ctrl-q" for a multi-key sequence, or a single character like "q".

Parameters

keysimpl Into<String>
Detach key specification.

attach_options.rlimit()

Set a POSIX resource limit with soft equal to hard for the attached process.

Parameters

Which resource to limit.
limitu64
Limit value (soft = hard).

attach_options.rlimit_range()

Set a resource limit with different soft and hard values for the attached process. build() errors if soft > hard.

Parameters

Which resource to limit.
softu64
Soft limit.
hardu64
Hard ceiling.

attach_options.build()

Finalize the options. Called automatically by attach_with(). Returns an error if any rlimit has soft > hard.

Returns

AttachOptions
Validated attach options.

ExecHandle

Returned by exec_stream() · exec_stream_with() · shell_stream() · shell_stream_with()

A handle to a running streaming execution. Receives ExecEvents as the process produces output, and provides control over stdin, signals, and the PTY size.

h.recv()

Receive the next event. None when the session has ended and all output has been delivered.

Returns

Option<ExecEvent>

h.wait()

Wait for the process to exit, discarding any remaining output.

Returns

Result<ExitStatus>

h.collect()

Wait for exit and collect all remaining stdout/stderr.

Returns

Result<ExecOutput>

h.id()

Session ID for this execution. Can be used to reattach later.

Returns

String

h.control()

A cloneable control handle for sending signals and resizes from another task.

Returns

ExecControl

h.take_stdin()

Take the stdin writer. Only available if stdin_pipe() was set; returns None after the first call.

Returns

Option<ExecSink>

h.signal()

Send a POSIX signal to the process (e.g. libc::SIGTERM).

Returns

Result<()>

h.kill()

Send SIGKILL to the process.

Returns

Result<()>

h.resize()

Resize the PTY for this session.

Returns

Result<()>

ExecControl

Returned by ExecHandle.control()

A cloneable, lightweight control handle for a streaming exec session. Lets a task other than the one owning the ExecHandle send signals and PTY resizes. Carries no event stream.

control.id()

Session ID for this execution.

Returns

String

control.signal()

Send a POSIX signal to the process (e.g. libc::SIGTERM).

Returns

Result<()>

control.kill()

Send SIGKILL to the process.

Returns

Result<()>

control.resize()

Resize the PTY for this session.

Returns

Result<()>

ExecSink

Returned by ExecHandle.take_stdin()

A writer for sending data to a running process’s stdin. Obtained via ExecHandle::take_stdin() after enabling stdin_pipe().

sink.write()

Write bytes to the process’s stdin.

sink.close()

Close stdin. The process sees EOF on its stdin.

ExecOutput

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

The result of a completed command execution. Holds the exit status and all captured output.

out.status()

Exit code and success flag.

Returns

ExitStatus

out.stdout()

Collected stdout decoded as UTF-8. Errors if the output is not valid UTF-8.

Returns

Result<String, FromUtf8Error>

out.stderr()

Collected stderr decoded as UTF-8.

Returns

Result<String, FromUtf8Error>

out.stdout_bytes()

Raw stdout bytes without decoding.

Returns

&Bytes

out.stderr_bytes()

Raw stderr bytes without decoding.

Returns

&Bytes

Types

ExecEvent

Yielded by ExecHandle.recv()

An event emitted by a streaming execution.

ExitStatus

Returned by ExecHandle.wait() · ExecOutput.status() · sb.wait() · sb.stop_and_wait()

The exit status of a completed process.

Rlimit

Configured via rlimit() · rlimit_range()

A POSIX resource limit. Built indirectly by rlimit() and rlimit_range() on the option builders; you rarely construct it by hand.

RlimitResource

Used by rlimit() · rlimit_range() · AttachOptionsBuilder.rlimit()

POSIX resource limit identifiers. Each maps to an RLIMIT_* constant.