sandbox.commands. Running a command on a paused sandbox transparently resumes it and executes.
run (synchronous)
Execute a command and wait for it to finish. Returns a CommandResult.
run (streaming)
Pass onStdout / on_stdout and/or onStderr / on_stderr callbacks. Output is delivered over Server-Sent Events and flushed to your callback as it arrives. The final result still contains the full concatenated output.
Streaming uses an idle timeout: the timer resets on every chunk. A command that keeps producing output never trips it, however long it runs. A command that goes silent still has to finish before the timeout.
spawn (interactive session)
spawn() hands back a CommandSession while the process is still running. Stream its output with callbacks, write to stdin, signal it, and call wait() for the result. Output comes over a WebSocket. The sessions guide walks through it.
In Python,
spawn() is on AsyncSandbox only; the synchronous version raises. It takes the same cwd, env, timeoutMs / timeout_seconds, onStdout, and onStderr options as run. Leave the timeout off for a long-lived process.CommandSession
| Member | Description |
|---|---|
stdin.write(data) | Write to stdin: a string (UTF-8) or raw bytes. |
stdin.close() | Close stdin, signalling EOF. |
kill(signal?) | Send a signal (default "SIGTERM"). |
wait() | Resolve with the CommandResult on exit; rejects if the connection drops first. |
close() | Kill the process and close the connection. |
await session.stdin.write(...), await session.kill(), await session.wait()) and the session is an async with context manager. In TypeScript the session supports await using for automatic cleanup.
Options
| Option | Type | Description |
|---|---|---|
cwd | string | Working directory. Server-side default when unset. |
env | Record<string, string> | Env vars for this command only (merged with sandbox-wide env vars). |
timeoutMs / timeout_seconds | number | Command timeout. Only sent when specified. |
onStdout / on_stdout | (data: string) => void | Stream stdout chunks. |
onStderr / on_stderr | (data: string) => void | Stream stderr chunks. |
signal | AbortSignal | TypeScript only. Aborts mid-execution. |
CommandResult
Non-zero exit codes
run() does not raise for non-zero exits. Inspect the exit code yourself.
Errors
Commonly raised:TimeoutError/SandboxTimeoutError: the timeout elapsed before the command finishedNotFoundError: the sandbox was deletedConflictError: the sandbox is in an invalid stateSandboxError: the connection dropped mid-stream without a terminalfinishedevent (streaming only)