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.
The shell command to execute.
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.
Working directory for the command.
Returns: ProcessResult
ProcessResult
ProcessResult is a dataclass with the following fields:
| Field | Type | Description |
|---|
exit_code | int | Exit code of the process (0 = success) |
stdout | str | Standard output |
stderr | str | Standard 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'
)