Skip to main content
Execute system commands in GravixLayer sandboxes with full control over arguments, environment variables, and execution context.

Basic Command Execution

Simple Commands

gravixlayer sandbox run YOUR_SANDBOX_ID echo --args="Hello from command line"
Output:
Command executed (exit code: 0)
Duration: 150ms
Success: True

STDOUT:
Hello from command line

System Information Commands

with Sandbox.create() as sandbox:
    result = sandbox.run_command("python", ["--version"])
    print(f"Python: {result.stdout}")
    
    result = sandbox.run_command("pwd")
    print(f"Directory: {result.stdout}")
    
    result = sandbox.run_command("ls", ["-la", "/home/user"])
    print(result.stdout)
Output:
Python: Python 3.11.14
Directory: /home/user
total 0
drwxr-xr-x 2 user user 4096 Oct 27 10:30 .
drwxr-xr-x 3 root root 4096 Oct 27 10:30 ..

Advanced Command Options

Working Directory

# Create a directory
gravixlayer sandbox run YOUR_SANDBOX_ID mkdir --args="-p" --args="/home/sandbox/project"

# Run command in specific directory
gravixlayer sandbox run YOUR_SANDBOX_ID ls --args="-la" --working-dir="/home/sandbox/project"

# Create file in specific directory
gravixlayer sandbox run YOUR_SANDBOX_ID touch --args="test.txt" --working-dir="/home/sandbox/project"

# Verify file creation
gravixlayer sandbox run YOUR_SANDBOX_ID ls --working-dir="/home/sandbox/project"
Output:
Command executed (exit code: 0)
Duration: 120ms
Success: True

Command executed (exit code: 0)
Duration: 95ms
Success: True

STDOUT:
total 8
drwxr-xr-x 2 user user 4096 Oct 28 10:30 .
drwxr-xr-x 3 user user 4096 Oct 28 10:30 ..
-rw-r--r-- 1 user user    0 Oct 28 10:30 test.txt

Command executed (exit code: 0)
Duration: 85ms
Success: True

STDOUT:
test.txt

Command Timeouts

# Command with timeout (3 second timeout for 10 second sleep - will timeout)
gravixlayer sandbox run YOUR_SANDBOX_ID sleep --args="10" --timeout=3000

# Quick command (should succeed within 1 second timeout)
gravixlayer sandbox run YOUR_SANDBOX_ID echo --args="Quick command" --timeout=1000
Output:
ERROR: {"error":"Failed to execute command: execution timed out"}

Command executed (exit code: 0)
Duration: 95ms
Success: True

STDOUT:
Quick command

Environment Variables

# Note: Environment variables are not directly supported in CLI run command
# Use the Python/JavaScript SDK for environment variable support
# Alternative: Set variables in shell command
gravixlayer sandbox run YOUR_SANDBOX_ID sh --args="-c" --args="export CUSTOM_VAR='custom_value' && echo \$CUSTOM_VAR"

# Check current environment
gravixlayer sandbox run YOUR_SANDBOX_ID env
Output:
Command executed (exit code: 0)
Duration: 110ms
Success: True

STDOUT:
custom_value

Command executed (exit code: 0)
Duration: 95ms
Success: True

STDOUT:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/root
USER=root
...

Simple File Commands

# Create a file
gravixlayer sandbox run YOUR_SANDBOX_ID touch --args="/home/sandbox/hello.txt"

# List files
gravixlayer sandbox run YOUR_SANDBOX_ID ls --args="/home/sandbox"

# Check file exists
gravixlayer sandbox run YOUR_SANDBOX_ID ls --args="-la" --args="/home/sandbox/hello.txt"

# Create directory and file
gravixlayer sandbox run YOUR_SANDBOX_ID mkdir --args="-p" --args="/home/sandbox/test"
gravixlayer sandbox run YOUR_SANDBOX_ID touch --args="/home/sandbox/test/example.txt"
gravixlayer sandbox run YOUR_SANDBOX_ID ls --args="-la" --args="/home/sandbox/test"
Output:
Command executed (exit code: 0)
Duration: 105ms
Success: True

Command executed (exit code: 0)
Duration: 85ms
Success: True

STDOUT:
hello.txt

Command executed (exit code: 0)
Duration: 90ms
Success: True

STDOUT:
-rw-r--r-- 1 root root 0 Oct 28 10:30 /workspace/hello.txt

Command executed (exit code: 0)
Duration: 95ms
Success: True

STDOUT:
total 8
drwxr-xr-x 2 root root 4096 Oct 28 10:30 .
drwxr-xr-x 3 root root 4096 Oct 28 10:30 ..
-rw-r--r-- 1 root root    0 Oct 28 10:30 example.txt

Next Steps