Commands resource provides shell command execution capabilities for running system commands, scripts, and tools inside sandboxes.
Accessing Commands Resource
Running Commands
run()
Execute a shell command and wait for completion.
command(str): Shell command to runtimeout(int): Command timeout in seconds (default: 30)background(bool): Run in background (returns immediately) (default: False)env(Dict[str, str], optional): Environment variables for this command onlyworking_dir(str): Working directory for command (default: “/workspace”)
CommandResult object
Raises:
CommandExecutionError: If command execution failsTimeoutError: If command times out
CommandResult
TheCommandResult object contains command execution results:
Basic Usage
Simple Commands
Commands with Output
Timeouts
Set custom timeouts for long-running commands:Working Directory
Execute commands in a specific directory:Environment Variables
Pass environment variables for a specific command:- Command-specific env vars (from
envparameter) - Global sandbox env vars (from
sandbox.env) - Agent default env vars
Background Execution
Run commands in the background:Error Handling
Handle command execution errors:Common Use Cases
Package Installation
File Operations
Git Operations
Process Management
System Information
Best Practices
1. Check Command Success
Always checkresult.success or result.exit_code:
2. Use Appropriate Timeouts
3. Handle Large Outputs
For commands with large output, consider redirecting to files:4. Use Environment Variables for Secrets
5. Chain Commands Carefully
Examples
Setup Project Environment
Data Processing Pipeline
Build and Deploy
Related
- Sandbox Class - Main sandbox class
- Code Execution - Execute code
- File Operations - File management
- Processes - Process management
- Environment Variables - Env var management

