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.
background=True to launch the command in the background and receive a CommandHandle immediately.
Shell command to execute inside the sandbox.
When
True, the API returns immediately and the method returns a
CommandHandle. When False (default), the method blocks and returns a
CommandResult.Additional environment variables for this command.
Unix user to run the command as inside the sandbox.
Working directory. Defaults to the user’s home directory.
Callback invoked for each line of stdout after the command completes.
For real-time streaming, use
run_stream() instead.Callback invoked for each line of stderr after the command completes.
Whether to attach a stdin pipe to the process.
Maximum time in seconds to wait for the command to complete.
Per-request HTTP timeout in seconds.
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.
Shell command to execute.
Called in real-time for each stdout chunk as it arrives from the SSE stream.
Called in real-time for each stderr chunk as it arrives.
Environment variables for the command.
Unix user to run as.
Working directory.
Command execution timeout in seconds.
CommandResult with the accumulated stdout and stderr after the command finishes.
sbx.commands.list()
List all running (background) processes in the sandbox.
Per-request HTTP timeout in seconds.
list[ProcessInfo]
sbx.commands.kill()
Send SIGKILL to a running process by PID.
Process ID of the command to kill.
Per-request HTTP timeout in seconds.
bool — True 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.
Process ID of the running command.
Data to write to stdin. Include
\n for newlines.Per-request HTTP timeout in seconds.
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.
Process ID of the running command.
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.
Called for each stdout line after the command completes.
Called for each stderr line after the command completes.
CommandResult. Raises CommandExitException if the exit code is non-zero.
handle.kill()
Kill the process.
Returns bool
handle.pid
Type int — the process ID.