Skip to main content
Run commands inside a running sandbox, collect their output, stream events live, or drive an interactive pseudo-terminal programmatically. See Commands for usage examples. The exec API mirrors os/exec conventions: a non-zero exit code is not a Go error. Transport, timeout, and spawn-failure paths return an error; a program that ran and exited non-zero is a normal *ExecOutput result, inspect Success() or ExitCode().

Sandbox methods

The exec entry points live on *Sandbox.

sb.Exec()

Run a command in the sandbox and return its collected output. Blocks until the command exits. The returned error is non-nil only on transport or runtime failures; a non-zero exit code is reported via ExecOutput.ExitCode, not as an error.

Parameters

ctxcontext.Context
Cancels the wait. The guest process may continue in the background.
cmdstring
Program to run. Passed literally to the guest agent (the image ENTRYPOINT is not consulted).
args[]string
Command arguments. May be nil.
Per-command cwd, timeout, TTY allocation, user, and env.

Returns

Collected stdout, stderr, and exit code.

sb.Shell()

Run /bin/sh -c command in the sandbox and collect its output. Blocks until the command exits. A convenience wrapper over Exec for shell one-liners.

Parameters

ctxcontext.Context
Cancels the wait.
commandstring
Shell command line passed to /bin/sh -c.
Per-command cwd, timeout, TTY allocation, user, and env.

Returns

Collected stdout, stderr, and exit code.

sb.ExecStream()

Start a streaming exec session and return an *ExecHandle. The handle MUST be closed with Close when the stream is no longer needed. Nonblocking: the handle returns immediately and the stream starts in the background. ctx controls only the start handshake; individual Recv calls take their own ctx. Non-zero exit codes are not errors, inspect ExecEventExited.

Parameters

ctxcontext.Context
Controls the start handshake only.
cmdstring
Program to run.
args[]string
Command arguments. May be nil.
Per-command options; add WithExecStdinPipe() to enable stdin and WithExecTTY(true) to allocate a pseudo-terminal.

Returns

Live exec session. Close it when done.

sb.ShellStream()

Run /bin/sh -c command with streaming output. A convenience wrapper over ExecStream. Nonblocking: the handle returns immediately and the stream starts in the background.

Parameters

ctxcontext.Context
Controls the start handshake only.
commandstring
Shell command line passed to /bin/sh -c.
Per-command options.

Returns

Live exec session. Close it when done.

ExecHandle methods

A live streaming exec session returned by ExecStream and ShellStream. Signal, Kill, and Resize may be called concurrently with Recv, allowing a control goroutine to act while another goroutine waits for terminal output. Other method combinations, including concurrent Recv calls, are not supported.

h.ID()

Return the unique identifier for this exec session, assigned by the guest agent. Useful for correlating log entries or referencing the session from external tooling.

Returns

string
Session correlation id.

h.TakeStdin()

Return the stdin sink for this exec session. Single-take: returns nil if the session was not started with WithExecStdinPipe, or if TakeStdin was already called on this handle (matching the Node and Python SDKs). The caller is responsible for closing the sink when done writing; closing the sink without closing the exec handle is fine, they own different Rust-side resources.

Returns

Stdin writer, or nil if unavailable.

h.Recv()

Block until the next event arrives or the stream ends. Returns an event with Kind == ExecEventDone when all events have been consumed. ctx controls the wait; cancellation causes Recv to return ctx.Err() immediately. The underlying Rust call may continue to completion in the background.

Parameters

ctxcontext.Context
Cancels the wait for the next event.

Returns

The next streamed event.

h.Collect()

Drain the stream, accumulate all output, and return it as an *ExecOutput. Equivalent to calling Recv in a loop and assembling the result. The handle should be closed after Collect returns.

Parameters

ctxcontext.Context
Cancels the drain.

Returns

Collected stdout, stderr, and exit code.

h.Wait()

Block until the process exits and return its exit code. Unlike Collect, stdout and stderr are discarded. The handle should be closed after Wait returns.

Parameters

ctxcontext.Context
Cancels the wait.

Returns

int
Process exit code.

h.Kill()

Send SIGKILL to the running process.

Parameters

ctxcontext.Context
Cancels the call.

h.Signal()

Send a Unix signal to the running process. Pass values from syscall (e.g. int(syscall.SIGTERM)).

Parameters

ctxcontext.Context
Cancels the call.
signalint
Signal number, e.g. int(syscall.SIGTERM).

h.Resize()

Resize the pseudo-terminal for this exec session. Use with a session started with WithExecTTY(true), typically when relaying a terminal-size change from a remote client. Resize may be called concurrently while another goroutine is blocked in Recv.

Parameters

ctxcontext.Context
Cancels the resize call.
rowsuint16
New terminal height in character cells.
colsuint16
New terminal width in character cells.

h.Close()

Release the Rust-side exec handle. Does not kill the running process; call Signal or Kill first if you need to terminate it. Safe to call after ExecEventDone has been received.

ExecOutput methods

The collected result of a completed command execution, returned by Exec, Shell, and Collect.

out.Stdout()

Captured standard output as a string.

Returns

string
Collected stdout.

out.StdoutBytes()

Captured standard output as raw bytes. Use when the output may not be valid UTF-8.

Returns

[]byte
Raw stdout bytes.

out.Stderr()

Captured standard error as a string.

Returns

string
Collected stderr.

out.StderrBytes()

Captured standard error as raw bytes.

Returns

[]byte
Raw stderr bytes.

out.ExitCode()

The process’s exit code, or -1 if the guest did not report one (e.g. the process was killed by a signal).

Returns

int
Exit code, or -1.

out.Success()

Reports whether the command exited with code 0.

Returns

bool
true if ExitCode() is 0.

ExecSink methods

A write-only pipe to a running process’s stdin, obtained from ExecHandle.TakeStdin. Implements io.WriteCloser. Write and Close use context.Background() under the hood; for caller-controlled cancellation use WriteCtx, or tear the session down via ExecHandle.Kill / Close.

sink.Write()

Send data to the process stdin. Implements io.Writer. Uses context.Background() internally, there is no way to cancel a stuck write through this method alone.

Parameters

p[]byte
Bytes to write to stdin.

Returns

int
Number of bytes written.

sink.WriteCtx()

Like Write, but with a caller-controlled context, so a stuck stdin write can be cancelled.

Parameters

ctxcontext.Context
Cancels the write.
p[]byte
Bytes to write to stdin.

Returns

int
Number of bytes written.

sink.Close()

Close the stdin sink. In non-TTY pipe mode this sends EOF to the process. In TTY mode it prevents further writes through this sink, but the guest PTY remains open; send the terminal’s EOF control character (usually \x04 in canonical mode) when the interactive program needs one. Implements io.Closer.

Types

ExecOutput

Returned by sb.Exec() · sb.Shell() · h.Collect()

The collected result of a command execution. A non-zero ExitCode is not treated as a Go error, callers inspect Success or ExitCode explicitly, matching how os/exec.Cmd.Output works against a script that exits non-zero. In TTY mode, Stdout contains the combined terminal output and Stderr is empty because a PTY cannot preserve separate stdout and stderr streams.

ExecHandle

Returned by sb.ExecStream() · sb.ShellStream()

A live streaming exec session. Must be closed with Close when done to release Rust-side resources. Signal, Kill, and Resize may run concurrently with Recv; other method combinations, including multiple concurrent Recv calls, are not supported.

ExecSink

Returned by h.TakeStdin()

A write-only pipe to a running process’s stdin. Implements io.WriteCloser.

ExecEvent

Returned by h.Recv()

One event from a streaming exec session. Kind identifies which fields are populated.

ExecEventKind

Field of ExecEvent

Identifies what an ExecEvent carries.

ExecFailure

Field of ExecEvent

Structured detail about a failed-to-start exec, populated on ExecEventFailed and ExecEventStdinError. See Error Handling for the kinds it carries (not_found, permission_denied, etc.) and how to branch on them.

ExecConfig

Populated by ExecOption

Configures a single Exec or ExecStream call. Most callers set fields through the WithExec* functional options; ExecConfig is exported for parity with the other SDKs’ config types.

ExecOption

Accepted by sb.Exec() · sb.Shell() · sb.ExecStream() · sb.ShellStream()

A functional option that mutates an ExecConfig. Construct them with the WithExec* functions below.

WithExecCwd()

Set the working directory for a single command.

Parameters

pathstring
Absolute path inside the guest.

WithExecTimeout()

Set a per-command timeout. When exceeded, the guest terminates the process and the call returns an error with Kind == ErrExecTimeout. Sub-second precision rounds up to whole seconds; pass at least 1 second.

Parameters

dtime.Duration
Timeout duration, rounded up to whole seconds.

WithExecStdinPipe()

Enable a stdin pipe for the exec session, allowing data to be written via ExecHandle.TakeStdin.

WithExecTTY()

Control whether the command runs inside a pseudo-terminal. Enable it for interactive programs such as shells, editors, REPLs, and top. Default: false. When enabled, the guest process runs with its stdin, stdout, and stderr connected to the PTY. Streaming sessions expose the combined terminal output through ExecEventStdout; they do not emit a separately attributable stderr stream. Collected output similarly places the combined stream in Stdout and leaves Stderr empty. Add WithExecStdinPipe() to write to the terminal programmatically, and use Resize to update its dimensions after the session starts.

Parameters

enabledbool
true to allocate a pseudo-terminal.

WithExecUser()

Run the command as the given guest user (UID or name).

Parameters

userstring
Guest user, as a UID or name.

WithExecEnv()

Add per-command environment variables. Called repeatedly, maps merge; later keys overwrite earlier ones.

Parameters

envmap[string]string
Environment variables for this command.