Keep programs running after you disconnect: screen and tmux

When your SSH connection drops, everything running in that session normally stops with it. That is a problem for long jobs such as big file copies, database imports and system upgrades. Screen and tmux solve it. They keep a terminal session running on the server, so you can disconnect, close your laptop, and later reconnect exactly where you left off.

This guide covers both tools: how to install them, the handful of keys you need, splitting the screen into panes, logging output, and when you should use a proper background service instead. If you are starting fresh, tmux is the more modern choice. Screen is older, simpler and installed on many servers already, so it is worth knowing both.

Low-cost VPS plans in stock right now

In stock when this page loaded. See all 20 matching plans

What is in this guide

A few words you will see

TermWhat it means
SessionA set of terminal windows that keeps running on the server.
DetachLeave a session running in the background and return to your normal prompt.
AttachReconnect to a running session.
WindowA full-screen terminal inside a session, like a browser tab.
PaneA part of a window, when you split it to see two terminals side by side. tmux only.
Prefix keyThe key combination you press before a command key. Ctrl+B in tmux, Ctrl+A in screen.
ScrollbackEarlier output that has scrolled off the screen.

Screen or tmux?

tmuxscreen
Prefix keyCtrl+BCtrl+A
Split into panesYes, easilyLimited
Settings file~/.tmux.conf~/.screenrc
Status barOn by defaultOff by default
Often already installedSometimesOften
Best forDay-to-day work and new usersQuick jobs on servers where it is already present

The most important job, keeping a session alive after you disconnect, works the same in both. Pick one and learn its five or six keys.

Install them

sudo apt update
sudo apt install tmux screen

Both use very little memory, so they suit any plan.

tmux: the essentials

In the key table, "Ctrl+B, then D" means press Ctrl and B together, let go, then press D.

What you wantCommand or keys
Start a named sessiontmux new -s work
Detach, leaving it runningCtrl+B, then D
List sessionstmux ls
Reattachtmux attach -t work
Reattach to the last sessiontmux attach
Scroll back through outputCtrl+B, then [ and use the arrow or Page Up keys. Press q to stop
End the sessionType exit in every window, or run tmux kill-session -t work

A typical use: log in, run tmux new -s copy, start a long rsync, press Ctrl+B then D, and log out. An hour later, log in and run tmux attach -t copy to see how it went. If your connection drops by accident, the session carries on in exactly the same way.

tmux: windows and panes

What you wantKeys
New windowCtrl+B, then C
Next or previous windowCtrl+B, then N or P
Go to window 1, 2 and so onCtrl+B, then the number
Split side by sideCtrl+B, then %
Split top and bottomCtrl+B, then "
Move between panesCtrl+B, then an arrow key
Close a paneType exit in it
Make one pane full size, and backCtrl+B, then Z

Panes are handy for watching a log in one half while you work in the other, for example sudo journalctl -f next to the commands you are running.

tmux: a small settings file

A few settings make tmux friendlier. Create ~/.tmux.conf:

set -g mouse on
set -g history-limit 10000
set -g base-index 1

This lets you click on panes and scroll with the mouse wheel, keeps more scrollback, and numbers windows from 1. Reload it inside tmux with Ctrl+B, then : and type source-file ~/.tmux.conf. With mouse mode on, hold Shift while selecting text to copy it in most terminal apps.

screen: the essentials

What you wantCommand or keys
Start a named sessionscreen -S work
Detach, leaving it runningCtrl+A, then D
List sessionsscreen -ls
Reattachscreen -r work
Reattach even if still attached elsewherescreen -d -r work
Scroll back through outputCtrl+A, then Esc, and use the arrow keys. Press Esc to stop
End the sessionType exit, or press Ctrl+A then K and confirm

If your connection dropped, screen may still think you are attached. screen -d -r work detaches the old connection and attaches the new one.

screen: windows and logging

What you wantKeys or command
New windowCtrl+A, then C
Next or previous windowCtrl+A, then N or P
List windowsCtrl+A, then "
Start logging the window to a fileCtrl+A, then H
Start a session with logging onscreen -L -S work

Logging saves everything shown in the window to a file named screenlog.0 in the folder where you started screen. It is useful for long jobs whose output you want to read later. With tmux, a simple way to get the same is to pipe a command through tee, for example long-command | tee job.log.

1 GB and 2 GB VPS plans in stock, good for learning the command line

In stock when this page loaded. See all 7 matching plans

Good uses

  • System upgrades. An upgrade that stops halfway because a connection dropped is painful to fix. Always run release upgrades inside tmux or screen. See upgrading an Ubuntu server safely.
  • Large copies and imports. rsync jobs, database imports and big downloads. See moving to a new VPS.
  • Watching while you work. A log in one pane and your commands in another.
  • Unreliable connections. Working from a train, a phone or patchy Wi-Fi.
  • Quick tests. Trying a program by hand before turning it into a service.

When to use a service instead

Screen and tmux keep sessions alive when you disconnect, but not when the server reboots, and they do not restart a program that crashes. For anything that should always be running, such as a bot, an app or a game server, use a systemd service instead. Create a file such as /etc/systemd/system/mybot.service:

[Unit]
Description=My bot
After=network.target

[Service]
User=alex
WorkingDirectory=/home/alex/mybot
ExecStart=/usr/bin/python3 /home/alex/mybot/bot.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

Change the user, folder and command to match your program. Then start it, and make it start at every boot:

sudo systemctl daemon-reload
sudo systemctl enable --now mybot
systemctl status mybot

Its output goes to the system journal, which you can read with sudo journalctl -u mybot -f. For apps packaged as containers, Docker's restart setting does the same job. See installing and using Docker on a VPS.

Troubleshooting

SymptomLikely causeFix
"no sessions" or "There is no screen to be resumed"The session ended, the server rebooted, or you are a different userRun tmux ls or screen -ls as the same user who started it
screen says the session is attachedThe old connection did not close cleanlyUse screen -d -r NAME
"sessions should be nested with care"You are already inside tmuxDetach first, or use the session you are in
The prefix keys do nothingYour terminal app or another tool uses those keysCheck your terminal's shortcuts, or change the prefix in the settings file
Cannot scroll with the mouseMouse mode is offUse the scrollback keys, or turn on mouse mode in tmux
A job stopped after a rebootSessions do not survive rebootsRun it as a systemd service instead
Strange characters or broken linesTerminal type mismatchTry a different terminal app, or set TERM=xterm-256color before starting

Limits

  • Not for always-on programs. Sessions end when the server reboots, and crashed programs stay crashed. Use systemd for those.
  • Sessions belong to one user. A session started with sudo, or as another user, is only visible to that user.
  • Forgotten sessions keep running. A session with a busy program left in it keeps using CPU and memory. Check tmux ls now and then.
  • Fair use applies. Keeping heavy jobs running for hours is fine for occasional work, but constant full CPU use can hit fair-use limits. Unmetered does not mean unlimited.

When to step up

Screen and tmux run on any plan. If you find yourself running long, heavy jobs in them often, such as builds, encoding or big data processing, the plan's CPU and memory may be the real limit. How to read VPS specs explains which specs matter for which jobs.

4 GB VPS plans in stock, for heavier jobs

In stock when this page loaded. See every plan

To compare what is available now, start with the cheapest RackNerd VPS guide and check the sales page. For more everyday tools, see useful Linux server tools.

More RackNerd guides