Execute Shell Commands
Run shell commands directly in your sandbox environment.
sandbox.execute(
command: str,
timeout: int = 10000
) -> ExecuteResultParameters
commandstringrequiredShell command to execute (e.g., ls -la, python -V).
timeoutintTimeout in milliseconds. Defaults to 10000 (10000ms).
Returns
stdoutstrStandard output from the command.
stderrstrStandard error from the command.
returncodeintExit code from the command (0 = success, non-zero = error).
commandstrThe original command that was executed.
Examples
Basic Example
from concave import Sandbox
sbx = Sandbox.create()
result = sbx.execute("uname -a")
print(f"Output: {result.stdout}")
print(f"Exit code: {result.returncode}")
sbx.delete()File Operations
from concave import Sandbox
sbx = Sandbox.create()
sbx.execute("echo 'Hello World' > /tmp/test.txt")
result = sbx.execute("cat /tmp/test.txt")
print(result.stdout) # Hello World
sbx.delete()With Timeout
from concave import Sandbox, SandboxTimeoutError
sbx = Sandbox.create()
try:
result = sbx.execute("sleep 5", timeout=2000)
except SandboxTimeoutError as e:
print(f"Timed out: {e}")
finally:
sbx.delete()Exceptions
SandboxValidationErrorExceptionRaised when the command is empty or invalid.
SandboxNotFoundErrorExceptionRaised when the sandbox no longer exists.
SandboxTimeoutErrorExceptionRaised when command execution exceeds the timeout duration.
SandboxConnectionErrorExceptionRaised when unable to connect to the sandbox service.
Error Handling
from concave import (
Sandbox,
SandboxValidationError,
SandboxTimeoutError
)
sbx = Sandbox.create()
try:
result = sbx.execute("invalid command")
print(result.stdout)
except SandboxValidationError as e:
print(f"Invalid command: {e}")
except SandboxTimeoutError as e:
print(f"Command timed out: {e}")
finally:
sbx.delete()