Terminal Multiplexers: Keeping Sessions Alive
Anyone who has worked on a remote server has felt this pain: you start a long job, your network drops or your laptop sleeps, and the terminal closes. The job dies with it, and hours of work are gone. A terminal multiplexer solves that problem. It runs a persistent session on the machine itself, so your programs keep going when you disconnect, and you can return to the same screen later.
The two best known multiplexers are tmux and GNU Screen. Both are open source, both are available on most Linux and macOS systems, and both follow the same core ideas. This article explains those ideas, using tmux for the examples.
Why sessions normally die
When you connect over SSH, the shell you get is tied to your connection. If the connection ends, the system sends a hangup signal to the shell, and it passes the signal on to the programs started from it. They exit.
A multiplexer sits between you and the shell. Its own process runs on the server and does not depend on your connection. Your terminal becomes a window that you can open and close onto that process. Programs inside it continue running whether anyone is watching or not.
Core concepts
Three layers organise everything in tmux.
- Session. A collection of work, such as one per project. A session survives until you kill it or the machine restarts.
- Window. Like a tab inside a session. Each window shows one full screen.
- Pane. A split of a window into several terminals, side by side or stacked.
You attach to a session to see it, and detach from it to leave it running in the background.
Basic commands
Start a named session:
tmux new -s build
Everything you type now happens inside tmux. To leave it running and return to your normal shell, press the prefix key, then d. The default prefix is Ctrl+B. So the sequence is Ctrl+B, release, then d.
List what is running and reconnect:
tmux ls
tmux attach -t build
If the SSH connection breaks, simply log in again and run the attach command. Your job will be exactly where you left it, with the output scrolled as before.
Windows and panes
Shortcuts all begin with the prefix key. These defaults cover most daily needs:
ccreates a new window.nandpmove to the next and previous window.,renames the current window.%splits the pane into left and right halves."splits the pane into top and bottom halves.- Arrow keys move between panes.
zzooms the current pane to full screen and back.[enters scroll mode, where you can use the arrows or page keys to read history, andqleaves it.
A typical layout for development is one pane with an editor, one running tests and one showing logs, all in the same window.
Common uses
- Long jobs. Builds, data transfers, database migrations and backups continue after you disconnect.
- Remote work. Resume exactly where you stopped after a network failure or a night's sleep.
- Organised projects. Keep one session per project, each with its own windows and working folders.
- Pairing and support. Two people can attach to the same session and see the same screen, which is useful for teaching or debugging together.
- Watching output. Keep a log tail or a monitoring command open in a pane while you work in another.
A starter configuration
tmux reads a file named .tmux.conf in your home folder. A few popular tweaks:
set -g mouse on
set -g history-limit 20000
set -g base-index 1
set -g prefix C-a
Turning on mouse support lets you click panes and scroll. A larger history limit keeps more output available. Starting window numbers at 1 matches the keyboard layout better. Some people change the prefix to Ctrl+A because it is easier to reach, but note that this conflicts with the "go to start of line" shortcut in the shell, so decide whether the trade is worth it. Reload the file in a running session with tmux source-file ~/.tmux.conf.
GNU Screen in brief
Screen is the older tool and does the same job with different keys. Start with screen -S build, detach with Ctrl+A then d, list with screen -ls, and reattach with screen -r build. It is often present on minimal systems where tmux is not installed, so it is worth knowing the basics.
Things to remember
- A multiplexer keeps programs alive across disconnects, but not across a server reboot. After a restart, sessions are gone, unless you use an add-on that saves and restores them.
- Nested sessions can be confusing. If you run tmux locally and also on a remote machine, keys may go to the wrong layer. Use a different prefix for one of them.
- Sessions that stay open hold memory and processes. Clean them up with
tmux kill-session -t nameonce the work is done. - For a single command you want to survive logout,
nohup command &is a lighter alternative that needs no setup, though you cannot interact with the process afterward.
Frequently Asked Questions
Is tmux or Screen better for a beginner?
Both work. tmux has a more active community, easier pane splitting and a more modern configuration. Screen is simpler and nearly universal on older systems. If you can choose, start with tmux.
Will my program keep running if I close the terminal window?
If it is running inside a multiplexer session, yes. Closing the terminal only detaches you. The session keeps running on the machine until you end it or the machine restarts.
Can I use tmux on Windows?
Yes, through Windows Subsystem for Linux, where it runs just as it does on Linux. It does not run natively in PowerShell, though Windows Terminal offers its own tabs and panes for local use.