Skip to main content
Run shell commands synchronously in your sandbox. Commands execute and return results immediately, making them perfect for quick operations, package installation, and system tasks.

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:
Expected Output:

Command Result

The CommandResult object contains:
  • stdout - Standard output from command
  • stderr - Standard error output (if any)
  • exit_code - Exit code (0 = success, non-zero = error)
  • success - Boolean indicating if command succeeded
  • execution_time - Time taken in seconds
Expected Output:

Package Installation

Install packages using package managers:
Expected Output:

Environment Variables

Pass environment variables to commands:
Expected Output:
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:
Expected Output:

Timeout Configuration

Set timeout for long-running commands:
Expected Output:
Default timeout is 30 seconds. Maximum timeout is typically 300 seconds (5 minutes) for synchronous execution. For longer-running commands, use Background Commands instead.

Error Handling

Handle command execution errors:
Expected Output:

Chaining Commands

Chain multiple commands together:
Expected Output:

Complete Example

Here’s a complete example showing command execution workflow:

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.

Next Steps