Skip to content

Interactive terminal

A pseudo-terminal (PTY) is useful when a program expects an interactive shell, terminal dimensions, or bidirectional input. Unlike commands.run, output is delivered while the process is still running.

mjs
import { Sandbox } from "@abox-dev/sdk";

const sandbox = await Sandbox.create();
const output = [];

try {
  const terminal = await sandbox.pty.create({
    cols: 80,
    rows: 24,
    onData: (data) => output.push(new TextDecoder().decode(data)),
  });

  await sandbox.pty.sendInput(
    terminal.pid,
    new TextEncoder().encode("printf 'hello from PTY\\n'\nexit\n"),
  );
  await terminal.wait();
  console.log(output.join(""));
} finally {
  await sandbox.kill();
}

Input is bytes. Include a trailing newline when it should act like pressing Enter. An interactive shell does not exit on its own, so send exit or kill the PTY before waiting for completion.

The PTY API can also resize, disconnect, reconnect, and terminate a session. See the commands reference for those operations and their timeout options.