Skip to main content
Run any shell command inside a sandbox using sandbox.commands.run().
This page covers run(). If you need to send input to a process while it runs, like a REPL, spawn() keeps the session open instead.
Using AsyncSandbox? The same methods are there, awaitable. See the Sandbox reference.

Handling exit codes

run() doesn’t throw on a non-zero exit code. Check exitCode on the result yourself.

Working directory and environment

Pass cwd and env to control the execution context.
Watch the units: TypeScript takes timeoutMs (milliseconds), Python takes timeout_seconds (seconds).
Commands run as the template’s user (the image’s USER, or the template’s user step). Pass user to run as someone else, like docker exec -u: a name or uid, optionally :group, e.g. user: "root" to install a package.

Timeouts

The timeout is a hard limit on how long a command runs. When it’s reached, the sandbox stops the command and run() returns with exit code 124. It doesn’t throw, so check the exit code. Output doesn’t extend the limit: a command that keeps printing still stops on time.
What happens depends on the method and whether you set a timeout: Set a timeout on any run() that can take longer than 30 seconds. For a process with no fixed end, like a server, use spawn() without one.
TypeScript rounds timeoutMs up to whole seconds. run() treats timeoutMs: 0 the same as leaving it out, and throws ValidationError for a value that is negative, not finite, or out of range.

Large output

Without callbacks, run() returns stdout and stderr in one JSON response, capped at 8 MiB of JSON-encoded output for both streams together. Characters that JSON escapes count for more than one byte (a < counts as six), so output with many of them is cut below 8 MiB of raw text. When the cap is reached, the sandbox drops the rest, sets truncated to true on the result, and appends a notice to stderr. The command itself still runs to the end.
For the full output, stream it with callbacks, or redirect it to a file in the sandbox and read the file. Streamed runs return everything, and truncated is always false. For longer commands, stream the output as it runs. To send input to a process while it runs, open a session.