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
Create a PTY session
Usesandbox.pty.create() to start an interactive bash shell.
The PTY runs an interactive bash shell with
TERM=xterm-256color, which supports ANSI colors and escape sequences.Timeout
PTY sessions have a configurable timeout that controls the session duration. The default is 60 seconds. For interactive or long-running sessions, settimeoutMs: 0 (JavaScript) or timeout=0 (Python) to keep the session open indefinitely.
Send input to PTY
UsesendInput() 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.
Resize the terminal
When the user’s terminal window changes size, notify the PTY withresize(). The cols and rows parameters are measured in characters, not pixels.
Disconnect and reconnect
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
- Implementing terminal session persistence
Kill the PTY
Terminate the PTY session withkill().
Wait for PTY to exit
Usewait() to wait for the terminal session to end (e.g., when the user types exit).
Interactive terminal (SSH-like)
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 samesandbox.pty API documented above.