In this lesson, you will learn how your shell detects and manages the sudden closure of a terminal session.
When you work in a terminal, you are interacting with a "Session" managed by the kernel. This session keeps track of the connection between your terminal emulator (like GNOME Terminal or iTerm2) and your shell process.
The Disconnect Lifecycle
When you close your terminal window or lose your internet connection while using SSH, the terminal emulator sends a specific signal to the shell: SIGHUP (Signal Hang Up).
Historically, this signal was designed to notify modems that the connection had been "hung up." When your shell receives SIGHUP, it interprets this as a command to clean up. It immediately notifies all of its child processes—the programs you are currently running—that the session is ending. By default, most programs react to this notification by terminating instantly. This is why, if you run a long-running script and accidentally close your terminal, the script dies along with it.
Why Sessions Matter
The shell acts as the "Session Leader." It maintains the tty (teletypewriter) device file, which is essentially the conduit for your input and output. When the tty disconnects, the shell loses its ability to print your prompt or receive your keystrokes.
If you want a process to survive a disconnect, you have to disconnect the process from the current session's SIGHUP chain. This is why tools like nohup (No Hang Up) exist—they intercept the SIGHUP signal before it reaches your program, telling the application to ignore the terminal closure entirely.
How it looks
If you run sleep 100 and close the window, the shell receives SIGHUP, kills the sleep process, and the session vanishes.
However, if you run:
nohup sleep 100 &
You are explicitly telling the shell: "If the session leader receives a hang-up signal, do not pass that signal to this process." The process survives, becomes an orphan, and is adopted by the system's init process, remaining alive in the background until it finishes its task.
Key Takeaway: A shell session acts as a parent that propagates termination signals to its children upon disconnect; preventing this requires decoupling your process from the session's hang-up signal.