Use this file to discover all available pages before exploring further.
E2B sandboxes provide multiple ways to run commands and interact with the terminal: commands.run() for executing commands, the PTY module for interactive terminal sessions, and SSH access for remote connectivity.
To run commands in background, pass the background option. This returns immediately and the command continues running in the sandbox. You can later kill the command using commands.kill().
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()// Start the command in the backgroundconst command = await sandbox.commands.run('echo hello; sleep 10; echo world', { background: true, onStdout: (data) => { console.log(data) },})// Kill the commandawait command.kill()
Use commands.list() to get all running processes in the sandbox, including background commands and PTY sessions.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()// Start a background processconst cmd = await sandbox.commands.run('sleep 300', { background: true })// List all running processesconst processes = await sandbox.commands.list()for (const proc of processes) { console.log(`PID: ${proc.pid}, Command: ${proc.cmd}`)}// Kill a process by PIDawait sandbox.commands.kill(processes[0].pid)
Background commands started with commands.run() appear as /bin/bash in the cmd field, not the inner command string. Only currently running processes are returned — exited or killed processes are not included.
The PTY (pseudo-terminal) module allows you to create interactive terminal sessions in the sandbox with real-time, bidirectional communication.Unlike commands.run() which executes a command and returns output after completion, PTY provides:
Real-time streaming - Output is streamed as it happens via callbacks
Bidirectional input - Send input while the terminal is running
Interactive shell - Full terminal support with ANSI colors and escape sequences
Session persistence - Disconnect and reconnect to running sessions
PTY sessions have a configurable timeout that controls the session duration. The default is 60 seconds. For interactive or long-running sessions, set timeoutMs: 0 (JavaScript) or timeout=0 (Python) to keep the session open indefinitely.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()const terminal = await sandbox.pty.create({ cols: 80, rows: 24, onData: (data) => process.stdout.write(data), timeoutMs: 0, // Keep the session open indefinitely})
Use sendInput() in JavaScript or send_stdin() in Python to send data to the terminal. These methods return a Promise (JavaScript) or complete synchronously (Python) - the actual output will be delivered to your onData callback.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()const terminal = await sandbox.pty.create({ cols: 80, rows: 24, onData: (data) => process.stdout.write(data),})// Send a command (don't forget the newline!)await sandbox.pty.sendInput( terminal.pid, new TextEncoder().encode('echo "Hello from PTY"\n'))
You can disconnect from a PTY session while keeping it running, then reconnect later with a new data handler. This is useful for resuming terminal sessions after network interruptions, sharing terminal access between multiple clients, or implementing terminal session persistence.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()// Create a PTY sessionconst terminal = await sandbox.pty.create({ cols: 80, rows: 24, onData: (data) => console.log('Handler 1:', new TextDecoder().decode(data)),})const pid = terminal.pid// Send a commandawait sandbox.pty.sendInput(pid, new TextEncoder().encode('echo hello\n'))// Disconnect - PTY keeps running in the backgroundawait terminal.disconnect()// Later: reconnect with a new data handlerconst reconnected = await sandbox.pty.connect(pid, { onData: (data) => console.log('Handler 2:', new TextDecoder().decode(data)),})// Continue using the sessionawait sandbox.pty.sendInput(pid, new TextEncoder().encode('echo world\n'))// Wait for the terminal to exitawait reconnected.wait()
Building a fully interactive terminal (like SSH) requires handling raw mode, stdin forwarding, and terminal resize events. For a production implementation, see the E2B CLI source code which uses the same sandbox.pty API documented above.