Overview
Synchronous command execution is ideal for:- Quick shell commands and system operations
- Package installation (pip, npm, apt, etc.)
- File system operations
- Environment setup and configuration
- Commands that complete in seconds or minutes
Commands run in
/bin/sh by default. For commands that complete quickly, synchronous execution is simpler than background execution.Basic Command Execution
Run a simple shell command:- Python
- JavaScript
Command Result
TheCommandResult object contains:
stdout- Standard output from commandstderr- Standard error output (if any)exit_code- Exit code (0 = success, non-zero = error)success- Boolean indicating if command succeededexecution_time- Time taken in seconds
- Python
- JavaScript
Package Installation
Install packages using package managers:- Python
- JavaScript
Environment Variables
Pass environment variables to commands:- Python
- JavaScript
Environment variables passed to
commands.run() have priority over global environment variables set via sandbox.env.set(). Priority: Request env > Global env > Agent env.Working Directory
Specify a custom working directory:- Python
- JavaScript
Timeout Configuration
Set timeout for long-running commands:- Python
- JavaScript
Error Handling
Handle command execution errors:- Python
- JavaScript
Chaining Commands
Chain multiple commands together:- Python
- JavaScript
Complete Example
Hereβs a complete example showing command execution workflow:- Python
- JavaScript
Best Practices
1
1. Use Appropriate Timeouts
Set timeouts based on expected execution time. Default 30 seconds is good for quick commands, but increase for package installations or long operations.
2
2. Handle Errors Gracefully
Always check
result.success and handle stderr appropriately. Use try/catch for exception handling.3
3. Use Environment Variables
Pass environment variables via
env parameter rather than hardcoding values in commands.4
4. Set Working Directory
Use
working_dir to run commands in the correct directory context.5
5. Use Background for Long Tasks
For commands that run longer than 5 minutes, use Background Commands instead.
Related
- Background Commands - Run commands in background
- Code Execution - Execute code directly
- SDK: sandbox.commands.run() - Python SDK method
- API: POST /commands/run - VM Agent API endpoint
Next Steps
- Learn about Background Commands for non-blocking execution
- Explore Code Execution for running code instead of shell commands
- Review Filesystem Operations for file management
Related
- CLI Commands - Shell commands from CLI

