Overview
Commands is the core capability for starting processes in FC Agent Sandbox. Agents typically use it for environment checks, dependency installation, code testing, starting services, and reading command output.
Common capabilities
-
sandbox.commands.run(): Run a command, either synchronously or in the background. -
sandbox.commands.list(): List running commands and PTY sessions. -
sandbox.commands.connect(): Connect to an existing background command. -
sandbox.commands.send_stdin()(Python) /sandbox.commands.sendStdin()(TypeScript): Write to a command's standard input. -
sandbox.commands.kill(): Stop a running command.
Minimal example
Python example:
from e2b import CommandExitException
try:
result = sandbox.commands.run("python3 --version")
print(result.stdout.strip())
except CommandExitException as error:
message = f"exit={error.exit_code}: {error.stderr}"
raise RuntimeError(message) from error
TypeScript example:
import { CommandExitError } from "e2b";
try {
const result = await sandbox.commands.run("python3 --version");
console.log(result.stdout.trim());
} catch (error) {
if (error instanceof CommandExitError) {
throw new Error(`exit=${error.exitCode}: ${error.stderr}`);
}
throw error;
}
When a foreground command exits with a non-zero status code, the Python SDK throws CommandExitException, and the TypeScript SDK throws CommandExitError. The exception object contains the exit code, standard output, and standard error.
Choose the right execution mode
Use synchronous run() for short commands, background commands for long-running tasks, and stdin or PTY for interactive tasks. When you need to expose a service externally, start it as a background command first, then get its access URL through Public Access.