Skip to main content

Start a process on a terminal

Set terminal in Spec (axectl -t) to run the process on a pseudo-terminal instead of pipes. The process becomes the leader of a new session with the terminal as its controlling terminal.
axectl sends your terminal’s size, or 80x24 when it is not running on a terminal.
On a terminal:
  • stdout and stderr are one stream, reported as stdout.
  • stdin is always open; you do not need stdin.
  • axed sets TERM to xterm-256color when neither the request nor axed’s environment sets it.
  • Process.terminal is true.
In the Agent Sandbox API, set pty (with cols and rows) on process.v1.ProcessService/Start. Without pty, that API always opens stdin.

Write to stdin

A process without a terminal has stdin only if you start it with stdin: true (axectl run -i). Otherwise stdin is /dev/null, and writes fail with FAILED_PRECONDITION. Processes.WriteInput writes bytes and returns the new stdin offset, the total number of bytes accepted so far. Process.stdin_offset reports the same number.
data is base64 in grpcurl’s JSON. Set eof: true to end input after the write. A pipe is closed. A terminal gets its end-of-file character (normally Ctrl-D) once, which a program reading at the start of a line takes as end of input, and accepts no input after it. The end-of-file character does not count toward the stdin offset, and ending input again does nothing, so a retried write with eof sends it only once.

Make retries safe with offsets

Name the stdin offset your bytes start at, and a retry after a lost reply cannot repeat input: Leave offset unset to append without the check. An offset so large that adding the data’s length overflows fails with INVALID_ARGUMENT. The offset counts every byte the process accepted, even when the write then fails, is cancelled, or is cut short because the process exited. A retry that names its original offset therefore writes only the bytes the process did not accept. A write can block when the process stops reading its input and the pipe or terminal buffer fills. Such a write stays pending until the process reads again or exits; if it exits first, the write ends with FAILED_PRECONDITION. For a stream of input, Processes.StreamInput takes a client stream of InputChunks. The first chunk must name the process; every chunk may carry data and eof. The call returns the final offset. StreamInput does not check offsets, so use WriteInput when you need safe retries.
The Agent Sandbox WriteStdin takes input or eof, with no offsets. As in sandboxd, its eof on a terminal process fails with FAILED_PRECONDITION; write 0x04 instead.

Resize

Processes.Resize sets the terminal’s cols and rows. It fails with FAILED_PRECONDITION when the process has no terminal or has exited.
axectl resizes for you in attach -i, run -a and exec, when it forwards your input from a terminal to a terminal process. It sends your size right away and again on every window change. A plain axectl attach without -i only reads output and does not resize. The Agent Sandbox equivalent is ResizeTTY.

Attach interactively

1

Start a process with input

Start it with a terminal (-t), or with -i to keep a pipe stdin open. -a attaches right away and implies -i.
2

Attach with input

attach replays the retained output, then follows. -i forwards your stdin. If both the process and your session are terminals, axectl switches your terminal to raw mode, so keys such as Ctrl-C reach the remote process instead of stopping axectl.
3

Detach

Press Ctrl-P Ctrl-Q. axectl prints axectl: detached from process <id>, exits 0, and the process keeps running. Attach again whenever you like.
axectl holds a Ctrl-P until the next key arrives. If that key is not Ctrl-Q, both keys go to the process. The detach keys are fixed; axectl has no option to change them. They work only when axectl forwards your input, in run -a and attach -i, and only when the process has a terminal. On a process without a terminal, every byte of input is forwarded unchanged, so binary input passes through intact. A plain axectl attach reads no input, so stop it with Ctrl-C; the process keeps running. axectl exec cannot detach, because its process dies with the client. When local stdin reaches end-of-file, axectl ends the process’s input. It closes the stdin of a process without a terminal, and sends the end-of-file character (Ctrl-D) to a terminal. This lets you pipe input, including to axectl exec -t:
The terminal acts on Ctrl-D only in its default line mode. A program that switches it to raw mode for line editing, as bash and python3 do, can miss a Ctrl-D that arrives before it reads, and then waits for more input. End piped input to such a program with a command:
If you run axectl through the Docker wrapper, piping input needs docker run -i rather than -it, because Docker refuses -t when its stdin is not a terminal.

Docker claims Ctrl-P Ctrl-Q

Docker uses Ctrl-P Ctrl-Q as its own default detach sequence for docker run -it. If you run axectl in a container, Docker sees the keys first and detaches the axectl container instead of axectl detaching from the process. Give Docker a different sequence with --detach-keys, as the axectl wrapper in the quickstart does.

Exit codes

axectl attach and axectl run -a exit with the process’s exit code once it exits, 0 when you detach, 130 when interrupted with Ctrl-C outside raw mode, and 125 when axectl itself fails. Signals, exits and limits explains how axed forms exit codes.