LFCS 58 ๐ง Persistent Sessions โ nohup, screen, tmux
A shell session ends when the terminal closes or the SSH connection drops. Every process that was started from that shell receives a SIGHUP (hang-up signal), and unless the process is specifically designed to survive it, it terminates. This is the default behavior, and for a quick command it is correct. For a long-running taskโa database migration, a compile, a training jobโit is a disaster. The three tools in this chapter solve the same problem in three different ways: nohup ignores the hang-up signal so the process survives, screen and tmux are terminal multiplexers that detach the entire session from the terminal so that it can be reattached later .
The distinction between nohup and the multiplexers is the distinction between “keep this one process alive” and “keep this entire terminal session alive.” nohup is a single command wrapper: nohup command & runs the command, redirects its output to nohup.out, and makes it immune to the hang-up signal. The process continues running, but you cannot reattach to it; you can only check it with ps or read its output file . screen and tmux create a virtual terminal that persists independently of the SSH session. You attach to the session, run commands, detach, log out, and later reattach to find the session exactly as you left it, with the command still running and its output still in the scrollback buffer .
Key point: nohup command & keeps a single process alive after logout by ignoring SIGHUP, but it cannot be reattached. screen and tmux are terminal multiplexers that persist an entire session; you detach with a key sequence, log out, and reattach later to find the command still running. The LFCS exam objective is “use terminal multiplexers for persistent sessions,” and tmux is the modern choice .
Why persistent sessions matter
The SSH disconnect problem. An SSH connection is not reliable. A laptop goes to sleep, a Wi-Fi network drops, a VPN times out, and the TCP connection closes. When the connection closes, the remote shell receives SIGHUP, and every process in its session is terminated. The nohup command is the minimal fix: it makes the process ignore the signal, so the disconnect does not kill it . The terminal multiplexers are the complete fix: they detach the session from the terminal entirely, so the terminal can close without affecting the session .
The reattach problem. nohup solves the “keep running” problem but not the “see what happened” problem. The process’s output goes to a file, and the only way to interact with it is to read the file or send it signals. A multiplexer solves both: the session persists, and you can reattach to it from any terminal, on any machine, as long as you can SSH to the host. The scrollback buffer, the running command, and the shell prompt are all preserved .
The multitasking problem. A single SSH session runs one foreground command at a time. A multiplexer lets you create multiple windows and panes within the same session. You can run a compile in one pane, tail a log in another, and keep a shell prompt in a third, all within the same detached session. This is the workflow benefit that screen and tmux provide beyond persistence .
The LFCS objective. The exam blueprint includes “use terminal multiplexers for persistent sessions” under the Essential Commands domain . The official objective explicitly mentions multiplexers, which means screen or tmux is the expected tool. nohup is a related command that appears in process management contexts, but it is not a multiplexer. The LFCS practice questions include installing tmux, which is a strong signal that tmux is the expected tool .
a. nohup: the minimal persistence wrapper
nohup (no hang-up) runs a command with SIGHUP ignored. The command continues after the terminal closes, and its output is redirected to nohup.out if stdout is a terminal.
# Run a script in the background with nohup
# The & puts it in the background
# The output goes to nohup.out by default
nohup ./backup.sh &
# The shell prints the PID of the background process
# [1] 12345
# Check the output file
cat nohup.out
# Check that the process is still running
ps -p 12345
The nohup.out file is created in the current directory if stdout is a terminal. If the current directory is not writable, nohup writes to $HOME/nohup.out instead . The explicit redirect is the better practice: nohup ./script.sh > output.log 2>&1 & sends both stdout and stderr to the named file, and the & puts the process in the background.
The limitation is the interaction model. nohup does not provide a way to reattach to the process. The process is running, but you cannot see its live output, send it input, or interact with it. For a batch job that produces a log file and does not need interaction, nohup is sufficient. For an interactive process, it is not.
b. screen: the classic multiplexer
screen is the older of the two multiplexers, and it is available on most distributions, though it may not be installed by default . The session is created with screen -S name, and the detach key is Ctrl+A followed by D.
# Install screen on Debian/Ubuntu
sudo apt install screen
# Create a named session
# The -S flag gives the session a name
screen -S backup
# Inside the session, run the long command
./backup.sh
# Detach from the session
# Press Ctrl+A, then D
# The session continues running in the background
# List the sessions
screen -ls
# Output: There is a screen on:
# 12345.backup (Detached)
# Reattach to the session
screen -r backup
The screen -ls command lists the sessions and their status. A detached session can be reattached with screen -r name. If the session is attached to another terminal, screen -d -r name detaches it from the other terminal and attaches it to the current one . The Ctrl+A is the command prefix, and the D is the detach command. The prefix is configurable in .screenrc, but the default is Ctrl+A.
The session persists until the command inside it exits or the session is killed. If the SSH connection drops, the session remains detached and can be reattached later . The scrollback buffer is preserved, so the output of the command is still visible after reattachment.
c. tmux: the modern multiplexer
tmux is the newer multiplexer, and it is the one the LFCS practice materials mention. The session is created with tmux new -s name, the detach key is Ctrl+B followed by D, and the reattach command is tmux attach -t name.
# Install tmux on Debian/Ubuntu
sudo apt install tmux
# Create a named session
# The new command creates a session
# The -s flag gives it a name
tmux new -s backup
# Inside the session, run the long command
./backup.sh
# Detach from the session
# Press Ctrl+B, then D
# The session continues running
# List the sessions
tmux ls
# Output: backup: 1 windows (created Mon Oct 19 12:00:00 2026)
# Reattach to the session
tmux attach -t backup
The Ctrl+B is the prefix key, and D is the detach command. The tmux ls command lists the sessions, and tmux attach -t name reattaches . The tmux new -s name creates the session with a name, which makes the reattach command explicit.
tmux provides more than screen: it has a more consistent command set, better scripting support, and a more active development community. The tmux session can be split into panes with Ctrl+B followed by % (vertical split) or " (horizontal split), and new windows can be created with Ctrl+B followed by C . The screen equivalent is Ctrl+A followed by | or S for splits, and Ctrl+A followed by C for a new window.
The LFCS practice materials include installing tmux, and the exam objective is “use terminal multiplexers” without naming a specific one . tmux is the safer choice because it is the more modern tool and the one the practice materials reference.
Complete Example Session
The following session demonstrates nohup for a batch job, screen for a named session, and tmux for a session with panes.
# ============================================
# PART 1: nohup FOR A BATCH JOB
# ============================================
# Run the backup script in the background
# The output is redirected to backup.log
# The & puts it in the background
nohup ./backup.sh > backup.log 2>&1 &
# The shell prints the PID
# [1] 12345
# The process continues after logout
# Check the log later
tail -f backup.log
# ============================================
# PART 2: screen FOR A NAMED SESSION
# ============================================
# Create a named session
screen -S compile
# Inside the session, run the compile
make -j4
# Detach with Ctrl+A, then D
# The session continues
# List the sessions
screen -ls
# 12345.compile (Detached)
# Reattach later
screen -r compile
# The compile is still running or finished
# The scrollback shows the output
# ============================================
# PART 3: tmux FOR A SESSION WITH PANES
# ============================================
# Create a named session
tmux new -s work
# Inside the session, split the window vertically
# Press Ctrl+B, then %
# The window is now two panes
# Run the compile in the left pane
make -j4
# Run the log tail in the right pane
# Press Ctrl+B, then the right arrow key to move to the right pane
tail -f compile.log
# Detach with Ctrl+B, then D
# The session continues with both panes running
# Reattach later
tmux attach -t work
# The compile and the log tail are both still there
The three parts cover nohup for a batch job, screen for a named session, and tmux for a session with panes. What the session demonstrates is that nohup is a single-command wrapper, while screen and tmux persist an entire session that can be reattached.
Quick Reference
Tool Comparison
| Tool | Persistence | Reattach | Multiplexing | Default Install |
|---|---|---|---|---|
nohup | Yes | No | No | Yes |
screen | Yes | Yes | Yes | Often |
tmux | Yes | Yes | Yes | Often |
nohup Commands
| Command | Effect |
|---|---|
nohup command & | Run in background, ignore SIGHUP |
nohup command > file 2>&1 & | Redirect output explicitly |
screen Commands
| Command | Effect |
|---|---|
screen -S name | Create named session |
Ctrl+A, D | Detach |
screen -ls | List sessions |
screen -r name | Reattach |
screen -d -r name | Detach from other terminal and reattach |
tmux Commands
| Command | Effect |
|---|---|
tmux new -s name | Create named session |
Ctrl+B, D | Detach |
tmux ls | List sessions |
tmux attach -t name | Reattach |
Ctrl+B, % | Split vertically |
Ctrl+B, " | Split horizontally |
Ctrl+B, C | New window |
Best Practices
โ Do This:
# Use nohup for simple batch jobs
nohup ./backup.sh > backup.log 2>&1 &
# Use tmux for interactive sessions
tmux new -s work
# Always name the session
tmux new -s compile
# Use tmux ls before attaching
tmux ls
โ Don’t Do This:
# Run a long job without nohup or a multiplexer
./backup.sh # dies on SSH disconnect
# Forget to detach before logout
# The session stays attached and the terminal is killed
# Use screen without installing it
screen -S work # command not found
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Job dies on logout | No nohup or multiplexer | nohup or tmux |
| Cannot reattach | Session was not detached | Detach before logout |
screen not found | Not installed | apt install screen |
tmux not found | Not installed | apt install tmux |
nohup.out in wrong directory | stdout not redirected | Redirect explicitly |
Real-World Examples
1. nohup batch job
nohup ./migrate.sh > migrate.log 2>&1 &
2. Create screen session
screen -S backup
3. Detach screen
# Ctrl+A, then D
4. Reattach screen
screen -r backup
5. Create tmux session
tmux new -s compile
6. Detach tmux
# Ctrl+B, then D
7. Reattach tmux
tmux attach -t compile
8. Split tmux pane
# Ctrl+B, then %
9. List tmux sessions
tmux ls
10. List screen sessions
screen -ls
Visual
nohup Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ nohup command & โ
โ โ โ
โ โผ โ
โ Process ignores SIGHUP โ
โ โ โ
โ โผ โ
โ SSH disconnects โ process continues โ
โ โ โ
โ โผ โ
โ Output in nohup.out or redirected file โ
โ No way to reattach โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
screen/tmux Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ screen -S name / tmux new -s name โ
โ โ โ
โ โผ โ
โ Virtual terminal created โ
โ โ โ
โ โผ โ
โ Detach (Ctrl+A,D / Ctrl+B,D) โ
โ โ โ
โ โผ โ
โ SSH disconnects โ session continues โ
โ โ โ
โ โผ โ
โ Reattach (screen -r / tmux attach) โ
โ Session is exactly as left โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Prefix Keys
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ screen: Ctrl+A โ
โ tmux: Ctrl+B โ
โ โ
โ Detach: prefix + D โ
โ New window: prefix + C โ
โ Split: prefix + % or " โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
nohup | Ignore SIGHUP, no reattach |
screen | Multiplexer, Ctrl+A prefix |
tmux | Multiplexer, Ctrl+B prefix |
| Detach (screen) | Ctrl+A, D |
| Detach (tmux) | Ctrl+B, D |
| Reattach (screen) | screen -r name |
| Reattach (tmux) | tmux attach -t name |
| LFCS objective | Use terminal multiplexers |
Key takeaways:
nohupkeeps a single process alive but cannot be reattached. It ignoresSIGHUP, redirects output tonohup.out, and continues after logout. The interaction is limited to reading the output file and sending signals .screenandtmuxpersist an entire session. You attach, run commands, detach, log out, and reattach later to find the session exactly as you left it .tmuxis the modern choice. It has a more consistent command set, better scripting, and more active development. The LFCS practice materials reference installingtmux.- The prefix keys are different.
screenusesCtrl+A, andtmuxusesCtrl+B. The detach command isDin both, but the prefix is the first thing to remember. - The LFCS objective is “use terminal multiplexers.” The exam expects
screenortmux, notnohup, becausenohupis not a multiplexer . nohupis still useful for batch jobs. A migration script that produces a log and does not need interaction is a good fit fornohup. An interactive process or a session that must be reattached is a good fit for a multiplexer.
Remember: A persistent session is a session that survives the terminal that started it. nohup is the minimal tool: it makes a single process ignore the hang-up signal, but it does not provide a way to reattach. screen and tmux are the complete tools: they create a virtual terminal that persists independently of the SSH connection, and they let you reattach to it later. The LFCS exam expects you to know the multiplexers, and tmux is the modern choice. The pattern is always the same: create a named session, run the command, detach, log out, reattach, and find the command still running.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!