Overview
Background command execution is perfect for:- Long-running commands (>5 minutes)
- Server processes that need to keep running
- Commands that don’t need immediate results
- Parallel execution of multiple commands
Background commands return immediately with a
process_id. Use process management to check status and manage background processes.Starting Background Commands
Start a command in the background:- Python
- JavaScript
Checking Process Status
Check the status of background commands:- Python
- JavaScript
Long-Running Servers
Start long-running server processes:- Python
- JavaScript
Environment Variables
Pass environment variables to background commands:- Python
- JavaScript
Timeout Configuration
Set timeout for background commands:- Python
- JavaScript
Complete Example
Here’s a complete example showing background command workflow:- Python
- JavaScript
Best Practices
1
1. Extract Process IDs
Extract process IDs from the result to track and manage background commands.
2
2. Set Appropriate Timeouts
Set timeouts based on expected execution time. Default is 30 seconds, but increase for longer tasks.
3
3. Monitor Status Regularly
Check process status periodically to track progress and detect failures early.
4
4. Use Process Management
Use
list_processes() and kill_process() to monitor and control background commands.5
5. Handle Failures
Check for failed status and access stderr for error details when commands fail.
Related
- Running Commands - Execute commands synchronously
- Process Monitoring - Monitor background processes
- SDK: sandbox.commands.run_background() - Python SDK method
- API: POST /commands/background - VM Agent API endpoint
Next Steps
- Learn about Running Commands for synchronous execution
- Explore Process Management to monitor background processes
- Review Code Execution for background code execution
Related
- CLI Commands - Shell commands from CLI

