Skip to main content
sbx.commands is the Commands sub-module available on every Sandbox instance. It provides methods to run foreground commands, launch background processes, stream real-time output, and interact with running processes via stdin.

sbx.commands.run()

Run a command and block until it completes, returning its stdout, stderr, and exit code.
Pass background=True to launch the command in the background and receive a CommandHandle immediately.
str
required
Shell command to execute inside the sandbox.
bool
default:"False"
When True, the API returns immediately and the method returns a CommandHandle. When False (default), the method blocks and returns a CommandResult.
dict[str, str] | None
default:"None"
Additional environment variables for this command.
str
default:"'user'"
Unix user to run the command as inside the sandbox.
str | None
default:"None"
Working directory. Defaults to the user’s home directory.
Callable[[str], None] | None
default:"None"
Callback invoked for each line of stdout after the command completes. For real-time streaming, use run_stream() instead.
Callable[[str], None] | None
default:"None"
Callback invoked for each line of stderr after the command completes.
bool | None
default:"None"
Whether to attach a stdin pipe to the process.
float | None
default:"60"
Maximum time in seconds to wait for the command to complete.
float | None
default:"None"
Per-request HTTP timeout in seconds.
Returns CommandResult when background=False, CommandHandle when background=True.

sbx.commands.run_stream()

Run a command with real-time SSE streaming. Callbacks are invoked as each chunk of output arrives, before the command completes.
str
required
Shell command to execute.
Callable[[str], None] | None
default:"None"
Called in real-time for each stdout chunk as it arrives from the SSE stream.
Callable[[str], None] | None
default:"None"
Called in real-time for each stderr chunk as it arrives.
dict[str, str] | None
default:"None"
Environment variables for the command.
str
default:"'user'"
Unix user to run as.
str | None
default:"None"
Working directory.
float | None
default:"60"
Command execution timeout in seconds.
Returns CommandResult with the accumulated stdout and stderr after the command finishes.

sbx.commands.list()

List all running (background) processes in the sandbox.
float | None
default:"None"
Per-request HTTP timeout in seconds.
Returns list[ProcessInfo]

sbx.commands.kill()

Send SIGKILL to a running process by PID.
int
required
Process ID of the command to kill.
float | None
default:"None"
Per-request HTTP timeout in seconds.
Returns boolTrue if the process was killed, False if it was already dead.

sbx.commands.send_stdin()

Write data to the stdin of a running background process.
int
required
Process ID of the running command.
str
required
Data to write to stdin. Include \n for newlines.
float | None
default:"None"
Per-request HTTP timeout in seconds.
Returns None

sbx.commands.connect()

Create a CommandHandle for an already-running process by PID without making an API call. Useful when you have a PID from list() and want to wait on it.
int
required
Process ID of the running command.
Returns CommandHandle

CommandHandle

CommandHandle is returned by sbx.commands.run(background=True) and sbx.commands.connect(). It provides methods to wait for or kill the process.

handle.wait()

Wait for the background command to complete.
Callable[[str], None] | None
default:"None"
Called for each stdout line after the command completes.
Callable[[str], None] | None
default:"None"
Called for each stderr line after the command completes.
Returns CommandResult. Raises CommandExitException if the exit code is non-zero.

handle.kill()

Kill the process. Returns bool

handle.pid

Type int — the process ID.

Data models

CommandResult

ProcessInfo

Stdout / Stderr

Line-level output models used when streaming output with timestamps:

Examples

Run a Python script

Run with environment variables

Stream long-running output

Background process with stdin