Appearance
2.8.2 — Terminals, TTYs & Job Control
You connect to the photo-upload server with ssh, start a long thumbnail rebuild, and go to lunch. Your laptop goes to sleep, the connection drops, and when you come back the rebuild has died halfway. Nothing crashed. Something killed it, on purpose.
Behind that is a piece of machinery older than most of computing: the terminal. This page explains where the words "terminal" and "TTY" come from, what really happens when you press Ctrl+C, Ctrl+Z or Ctrl+D, why a dropped connection kills the programs you started, and how to keep a job running after you leave.
1. A living fossil: the teletypewriter
In the 1960s and 70s, you did not sit at a screen. You sat at a teletypewriter, a keyboard joined to a printer, connected to a shared computer by a cable or a telephone line. You typed a command. The characters were sent to the computer. The computer's reply was printed on paper in front of you. The early Unix machines at Bell Labs were driven from teletypes such as the Teletype Model 33. The abbreviation for teletypewriter was TTY.
When screens replaced paper in the 1970s, the software kept working exactly as before, pretending the screen was a very fast teletype. Machines such as DEC's VT100, from 1978, added special sequences of characters that meant "move the cursor", "clear the screen" or "switch to red text". Those escape sequences are still how every terminal is controlled today.
Today there is no hardware at all. The Terminal app on a Mac, Windows Terminal, or the terminal pane in your editor is a terminal emulator: an ordinary program that draws a window and pretends to be a 1978 terminal. The kernel still contains a TTY layer that sits between that emulator and the shell, carrying fifty-year-old ideas and names forward. When you see tty, pts or TERM=xterm-256color, you are seeing that history.
Escape sequences in your own programs
Coloured output in a terminal is nothing but those escape sequences. Here the upload service prints a failure in red, but only when a person is watching.
ts
// upload-status.ts — colour the output only when a person is watching
const toTerminal = process.stdout.isTTY === true; // (1) is stdout a terminal?
const red = (s: string) => (toTerminal ? `\x1b[31m${s}\x1b[0m` : s); // (2) escape codes for red
console.log(`stdout is a terminal: ${toTerminal}`);
console.log(`upload 4012: ${red("FAILED")} (storage timeout)`);
console.log("bytes written:", JSON.stringify(red("FAILED"))); // (3) show exactly what was sent
// Run with its output piped into another program (node upload-status.ts | cat):
// stdout is a terminal: false
// upload 4012: FAILED (storage timeout)
// bytes written: "FAILED"
//
// Run directly in a terminal, the last line instead shows the hidden codes:
// bytes written: "\u001b[31mFAILED\u001b[0m"(1)isTTYasks the kernel whether descriptor 1 is a terminal or something else, such as a pipe or a file (2.8.1).(2)\x1bis the escape character, number 27.\x1b[31mmeans "switch to red" and\x1b[0mmeans "back to normal". They are the VT100's codes.(3)Printing the string as JSON shows the invisible characters. When the output goes to a terminal, the codes are there and the terminal turns them into colour. When it goes to a pipe or a log file, the program leaves them out, because a log file full of\x1b[31mis unreadable.
That is why tools like ls, grep and git colour their output on screen but not when you pipe it. They are asking the same question.
2. Pseudo-terminals: how a terminal works without hardware
With no physical terminal, the kernel creates a pair of connected devices called a pseudo-terminal, or pty.
- One end, the controller, belongs to the terminal emulator, or to the
sshserver. - The other end, which appears as a device file such as
/dev/pts/3, is handed to the shell as its stdin, stdout and stderr.
When you ssh into the upload server, the ssh server program on that machine asks the kernel for a new pseudo-terminal, starts your shell attached to it, and relays bytes between it and the network. That is why tty on a remote machine prints something like /dev/pts/0.
3. The line discipline: Backspace, Enter and Ctrl+C
Between the two ends sits a small part of the kernel called the line discipline. It is why typing at a shell prompt feels the way it does.
It collects a whole line before the program sees it. When you type ls -la and make a mistake, Backspace deletes a character. The program never sees the mistake or the Backspace. The line discipline edits the line itself, and only when you press Enter does the program's read return the finished line. This is called canonical mode.
Programs can turn that off. A text editor such as vim, a password prompt or a full-screen tool like top needs every key the moment it is pressed, without echoing it to the screen. They switch the terminal into raw mode. In Node, process.stdin.setRawMode(true) does exactly this. If such a program crashes without switching back, your terminal is left in raw mode and behaves strangely. The command reset puts it right.
It turns a few keys into signals. This is the part that matters most.
| Key | What the line discipline does | Default effect |
|---|---|---|
| Ctrl+C | sends SIGINT | stop the program |
| Ctrl+\ | sends SIGQUIT | stop it, with a core dump |
| Ctrl+Z | sends SIGTSTP | pause it |
| Ctrl+D | ends the input | the program's read returns 0 bytes |
So Ctrl+C is not a message to the program. The kernel sees the key and sends the signal SIGINT (2.2.3), which a program can catch, which is how the upload service's graceful shutdown handled Ctrl+C. Ctrl+D is different: it is not a signal at all. It tells the line discipline "the input has ended", so the program's next read returns 0 bytes, which every program treats as end of input (2.1.2). That is why Ctrl+D closes a shell or ends the input to a program reading the keyboard.
4. Process groups, sessions and the foreground job
Which program receives the SIGINT when you press Ctrl+C? If you typed grep " 500 " access.log | sort | head, there are three processes. They should all stop together. So the kernel groups processes.
- A process group is a set of processes that receive terminal signals together. The shell puts every process of one pipeline in one group. That group is a job.
- A session is everything started from one login or one terminal window: the shell and all its jobs.
- Each session has at most one controlling terminal, and at any moment one of its process groups is the foreground group.
When you press Ctrl+C, the line discipline sends SIGINT to every process in the foreground group, and to no one else. The shell itself is not in that group, so it survives and shows the next prompt.
Job control: running things in the background
A job does not have to be in the foreground. These are the shell's job-control tools:
sh
./rebuild-thumbnails.sh & # start it in the background; the prompt returns at once
jobs # list this shell's jobs: [1]+ Running ./rebuild-thumbnails.sh &
./import-photos.sh # start another job, in the foreground
# press Ctrl+Z # SIGTSTP: the import job is paused
bg # resume the paused job in the background
fg %1 # bring job 1 back to the foregroundA background job keeps running, but it is not the foreground group, so Ctrl+C does not reach it. If a background job tries to read from the terminal, the kernel pauses it with a signal, because only the foreground job may read your typing. Ctrl+Z pauses the foreground job with SIGTSTP, and the process shows state T, stopped, in the table from 2.2.1. fg and bg resume it, by sending SIGCONT.
5. Why closing the terminal kills your programs
Now the lunchtime mystery. When the terminal goes away, whether you close the window or your ssh connection drops, the kernel sends SIGHUP, "hang up", to the session's leading process, normally the shell. The name comes straight from the telephone-line days: the line has hung up. The shell then passes SIGHUP on to its jobs, and the default action for SIGHUP is to end the process. So the thumbnail rebuild died, as designed. In the old days that was the right behaviour: a user who hung up the phone would never see the results anyway.
There are several ways to keep a job running after you leave, from quick fixes to the proper solution.
nohup.nohup ./rebuild-thumbnails.sh &starts the program withSIGHUPignored, and sends its output to a file callednohup.out, because the terminal will not be there to show it.disown. After starting a job in the background,disowntells the shell to forget it, so the shell will not passSIGHUPon.- A terminal multiplexer,
tmuxorscreen. These run a long-lived program on the server that owns its own pseudo-terminals. Your shell runs inside it. When your connection drops,tmuxand everything inside it keep running, and when you reconnect,tmux attachputs you back exactly where you were, with the output still on the screen. For interactive work on servers, this is the everyday tool. - Make it a proper service. Anything that should run continuously, like the upload service itself, should not be started from a terminal at all. It should be started and supervised by the system's service manager, systemd, which restarts it if it fails and keeps its logs. Page 2.8.4 shows how.
What the interviewer will push on
"What does TTY mean, and what is a pseudo-terminal?" They are checking that you know the history and the mechanism. TTY is short for teletypewriter, the keyboard-and-printer terminals of early Unix. A pseudo-terminal is a kernel-made pair of devices, one end held by the terminal emulator or ssh server and the other given to the shell, standing in for the old hardware.
"What actually happens when you press Ctrl+C?" The kernel's line discipline sees the key and sends SIGINT to every process in the terminal's foreground process group. The program does not receive a keystroke; it receives a signal, which it can catch. The shell is not in that group, so it survives.
"Is Ctrl+D a signal?" No. It tells the line discipline that input has ended, so the program's next read returns 0 bytes, which programs treat as end of file. This is a favourite trick question.
"Why does my program die when my SSH session drops, and how do I prevent it?" The kernel sends SIGHUP to the session, the shell passes it to its jobs, and the default action ends them. Use nohup or disown for a one-off, tmux or screen for interactive work you want to return to, and a systemd service for anything that should keep running.
"Why do tools stop colouring their output when piped?" They check whether stdout is a terminal, isatty, and only emit escape sequences when it is, so files and pipes get clean text.
Something to volunteer that nobody asks for: a program that leaves the terminal in raw mode after crashing makes the terminal echo nothing or ignore Enter. Typing reset blind, and pressing Enter, fixes it. Knowing that shows you have actually lived in terminals.
Recall
- TTY comes from the teletypewriter, a keyboard and printer joined to a shared computer. VT100-era escape sequences still control colours and the cursor; today's terminal is a terminal emulator, and programs check
isTTYbefore emitting colour codes. - A pseudo-terminal is a kernel pair of devices: the emulator or
sshserver holds one end, the shell gets the other, such as/dev/pts/3. - The line discipline collects lines and handles Backspace in canonical mode (editors switch to raw mode), and turns Ctrl+C into
SIGINT, Ctrl+\ intoSIGQUITand Ctrl+Z intoSIGTSTP. Ctrl+D is not a signal; it ends input, soreadreturns 0. - Each pipeline is a process group, or job, inside a session with one controlling terminal. Terminal signals go to the foreground group only.
&,jobs,fgandbgmanage jobs. - When the terminal goes away, SIGHUP ends the session's jobs. Keep work alive with
nohup,disown,tmux/screen, or properly as a systemd service.
Self-test: Why is the word TTY still used when there is no teletypewriter? What does the line discipline do between your keyboard and the shell? Which processes receive SIGINT when you press Ctrl+C during a pipeline? What does Ctrl+D actually do? Why did the thumbnail rebuild die when the connection dropped, and what are two ways to stop that?
Quiz Bank
FoundationalWhat do the words terminal and TTY mean today?
A TTY was originally a teletypewriter: a keyboard and printer connected to a shared computer, which printed the computer's replies on paper. When screens replaced paper, software kept treating them as fast teletypes, and the names stayed. Today a "terminal" is usually a terminal emulator, an ordinary program that draws a window and imitates a hardware terminal, connected to the shell through a kernel pseudo-terminal. The kernel's TTY layer between them still provides line editing and turns special keys into signals.
FoundationalWhat do Ctrl+C, Ctrl+Z and Ctrl+D do?
They are handled by the kernel's line discipline, not by the program. Ctrl+C sends SIGINT to every process in the terminal's foreground process group, which by default ends them, though programs can catch it. Ctrl+Z sends SIGTSTP, pausing the foreground job so the shell can later resume it with fg or bg. Ctrl+D is not a signal: it marks the end of input, so the program's next read returns 0 bytes, which programs treat as end of file, and which makes a shell exit.
AppliedWhat is a process group, and why does Ctrl+C stop every program in a pipeline but not the shell?
A process group is a set of processes that receive terminal-generated signals together. The shell puts all the processes of one pipeline, such as grep | sort | head, in one group, which is one job, and makes that group the terminal's foreground group. When you press Ctrl+C, the line discipline sends SIGINT to every process in the foreground group, so the whole pipeline stops together. The shell itself is in a different group, so it does not receive the signal, and it prints the next prompt.
InterviewWhy do programs started from an SSH session die when the connection drops, and how do you keep them running?
When the connection drops, the pseudo-terminal goes away and the kernel sends SIGHUP to the session's leader, the shell, which passes it on to its jobs. SIGHUP's default action ends the process, so the jobs die.
To keep a job running, there are three quick options. Start it with nohup, which ignores SIGHUP and writes output to nohup.out. Or disown it after starting it in the background. Or run your work inside tmux or screen, which keep running on the server and let you reattach later. Anything meant to run continuously should instead be a service supervised by systemd, which starts it independently of any terminal and restarts it if it fails.
InterviewWhat is the difference between canonical and raw terminal mode?
In canonical mode, the default, the line discipline collects input into lines: it echoes characters, handles Backspace and other editing keys itself, and hands the program a complete line only when Enter is pressed. In raw mode, each keystroke is passed to the program immediately, with no echo, no line editing and, often, no special handling of keys such as Ctrl+C. Editors, password prompts and full-screen tools use raw mode. A program that crashes in raw mode leaves the terminal misbehaving until it is reset, for example with the reset command.
StaffA team runs a long data migration by SSH-ing into a server and starting it in the foreground. It occasionally dies halfway with no error. Explain the likely cause and propose a robust way to run such jobs.
The likely cause is a dropped connection. When the laptop sleeps or the network blips, the pseudo-terminal disappears, the kernel sends SIGHUP to the shell, the shell forwards it to the migration, and the default action ends it. Nothing is logged by the migration itself because it never got a chance to report.
A robust approach removes the dependence on the terminal. For one-off interactive work, run inside tmux or screen so the job survives disconnection and can be reattached. Better, run the migration as a supervised unit of work: a systemd unit or a one-off job in the team's scheduler, with its output going to a log, so its exit code and logs are recorded. Make the migration itself resumable or idempotent, so that if it is interrupted for any reason, it can be restarted safely from where it stopped.
Scenario Drill
DrillAfter running a misbehaving command-line tool on the upload server, your terminal stops showing what you type and Enter seems to do nothing. Using this page, explain what happened and how to recover.
The tool switched the terminal into raw mode, turning off echo and line handling so it could read keys one at a time, and then crashed or was killed before switching back. The line discipline is still in raw mode, so your keystrokes are not echoed, and Enter may not produce the line ending the shell expects.
To recover, type reset and press Enter, even though you cannot see the letters, or press Ctrl+J, which sends a raw line ending, then type reset and press Ctrl+J again. reset restores the terminal's normal settings. If that fails, stty sane does the same job. If you write such tools yourself, restore the terminal mode in a handler that runs on exit and on signals such as SIGINT and SIGTERM, so a crash does not leave the user's terminal broken.
Next: the kernel exposes a remarkable amount of its own state as files you can simply read: every process's memory map, every open descriptor, the CPU's details, and the counts behind every monitoring tool. Page 2.8.3 explores /proc, /sys and /dev, where Unix's "everything is a file" becomes literal.