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
- 1 GB KVM VPS Special1 CPU · 1 GB RAM · 20 GB disk · 3 TB bandwidth · New York, Los Angeles, Dallas$21.99/yrabout $1.83/moView at RackNerd
- KVM-512MB1 CPU · 512 MB RAM · 30 GB disk · 500 GB bandwidth · Los Angeles, Dallas, New York$26.99/yrabout $2.25/moView at RackNerd
- Ryzen-512MB (Linux)1 CPU · 512 MB RAM · 10 GB disk · 500 GB bandwidth · New York, Chicago, Seattle$26.99/yrabout $2.25/moView at RackNerd
- Storage VPS Special - 100GB Storage1 CPU · 1 GB RAM · 100 GB disk · 10 TB bandwidth · Los Angeles, Dallas, New York$35.00/yrabout $2.92/moView at RackNerd
In stock when this page loaded. See all 20 matching plans
What is in this guide
- A few words you will see
- Screen or tmux?
- Install them
- tmux: the essentials
- tmux: windows and panes
- tmux: a small settings file
- screen: the essentials
- screen: windows and logging
- Good uses
- When to use a service instead
- Troubleshooting
- Limits
- When to step up
A few words you will see
| Term | What it means |
|---|---|
| Session | A set of terminal windows that keeps running on the server. |
| Detach | Leave a session running in the background and return to your normal prompt. |
| Attach | Reconnect to a running session. |
| Window | A full-screen terminal inside a session, like a browser tab. |
| Pane | A part of a window, when you split it to see two terminals side by side. tmux only. |
| Prefix key | The key combination you press before a command key. Ctrl+B in tmux, Ctrl+A in screen. |
| Scrollback | Earlier output that has scrolled off the screen. |
Screen or tmux?
| tmux | screen | |
|---|---|---|
| Prefix key | Ctrl+B | Ctrl+A |
| Split into panes | Yes, easily | Limited |
| Settings file | ~/.tmux.conf | ~/.screenrc |
| Status bar | On by default | Off by default |
| Often already installed | Sometimes | Often |
| Best for | Day-to-day work and new users | Quick 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 want | Command or keys |
|---|---|
| Start a named session | tmux new -s work |
| Detach, leaving it running | Ctrl+B, then D |
| List sessions | tmux ls |
| Reattach | tmux attach -t work |
| Reattach to the last session | tmux attach |
| Scroll back through output | Ctrl+B, then [ and use the arrow or Page Up keys. Press q to stop |
| End the session | Type 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 want | Keys |
|---|---|
| New window | Ctrl+B, then C |
| Next or previous window | Ctrl+B, then N or P |
| Go to window 1, 2 and so on | Ctrl+B, then the number |
| Split side by side | Ctrl+B, then % |
| Split top and bottom | Ctrl+B, then " |
| Move between panes | Ctrl+B, then an arrow key |
| Close a pane | Type exit in it |
| Make one pane full size, and back | Ctrl+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 want | Command or keys |
|---|---|
| Start a named session | screen -S work |
| Detach, leaving it running | Ctrl+A, then D |
| List sessions | screen -ls |
| Reattach | screen -r work |
| Reattach even if still attached elsewhere | screen -d -r work |
| Scroll back through output | Ctrl+A, then Esc, and use the arrow keys. Press Esc to stop |
| End the session | Type 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 want | Keys or command |
|---|---|
| New window | Ctrl+A, then C |
| Next or previous window | Ctrl+A, then N or P |
| List windows | Ctrl+A, then " |
| Start logging the window to a file | Ctrl+A, then H |
| Start a session with logging on | screen -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
- 1 GB KVM VPS Special1 CPU · 1 GB RAM · 20 GB disk · 3 TB bandwidth · New York, Los Angeles, Dallas$21.99/yrabout $1.83/moView at RackNerd
- Storage VPS Special - 100GB Storage1 CPU · 1 GB RAM · 100 GB disk · 10 TB bandwidth · Los Angeles, Dallas, New York$35.00/yrabout $2.92/moView at RackNerd
- 2 GB KVM VPS Special2 CPU · 2 GB RAM · 35 GB disk · 5 TB bandwidth · New York, Los Angeles, Dallas$35.99/yrabout $3.00/moView at RackNerd
- Ryzen-1GB (Linux)1 CPU · 1 GB RAM · 15 GB disk · 1 TB bandwidth · New York, Chicago, Seattle$194.29/yrabout $16.19/moView at RackNerd
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
| Symptom | Likely cause | Fix |
|---|---|---|
| "no sessions" or "There is no screen to be resumed" | The session ended, the server rebooted, or you are a different user | Run tmux ls or screen -ls as the same user who started it |
| screen says the session is attached | The old connection did not close cleanly | Use screen -d -r NAME |
| "sessions should be nested with care" | You are already inside tmux | Detach first, or use the session you are in |
| The prefix keys do nothing | Your terminal app or another tool uses those keys | Check your terminal's shortcuts, or change the prefix in the settings file |
| Cannot scroll with the mouse | Mouse mode is off | Use the scrollback keys, or turn on mouse mode in tmux |
| A job stopped after a reboot | Sessions do not survive reboots | Run it as a systemd service instead |
| Strange characters or broken lines | Terminal type mismatch | Try 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 lsnow 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
- 4 GB KVM VPS Special3 CPU · 4 GB RAM · 60 GB disk · 7 TB bandwidth · New York, Los Angeles, Dallas$59.99/yrabout $5.00/moView at RackNerd
- Ryzen-4GB (Linux)2 CPU · 4 GB RAM · 30 GB disk · 3 TB bandwidth · New York, Chicago, Seattle$265.57/yrabout $22.13/moView at RackNerd
- KVM-4GB4 CPU · 4 GB RAM · 130 GB disk · 3 TB bandwidth · Los Angeles, Dallas, New York$295.08/yrabout $24.59/moView at RackNerd
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.