Skip to main content
The Commands module lets you run shell commands inside a sandbox and capture their output. Access it via sandbox.commands.

Running a Command

result = await sandbox.commands.run('echo "Hello, World!"')

print(result.stdout)     # Hello, World!
print(result.stderr)     # (empty)
print(result.exit_code)  # 0

await sandbox.commands.run(command, **kwargs)

Executes a shell command and waits for it to complete.
command
str
required
The shell command to execute.
timeout
int
default:"60"
Maximum execution time in seconds. The command is killed if it exceeds this.
env
dict[str, str] | None
default:"None"
Additional environment variables for this command.
cwd
str | None
default:"None"
Working directory for the command.
Returns: ProcessResult

ProcessResult

ProcessResult is a dataclass with the following fields:
FieldTypeDescription
exit_codeintExit code of the process (0 = success)
stdoutstrStandard output
stderrstrStandard error

Examples

Run with a Custom Working Directory

result = await sandbox.commands.run('ls -la', cwd='/app')

Run with Environment Variables

result = await sandbox.commands.run(
    'echo $MY_VAR',
    env={'MY_VAR': 'hello'},
)
print(result.stdout)  # hello

Run with a Timeout

result = await sandbox.commands.run('sleep 30', timeout=5)
print(result.exit_code)  # non-zero (killed)

Check for Errors

result = await sandbox.commands.run('python3 script.py')

if result.exit_code != 0:
    print(f"Command failed: {result.stderr}")

Chain Commands

result = await sandbox.commands.run(
    'cd /app && pip install -r requirements.txt && python3 main.py'
)