Log inSign up

Run long jobs in tmux so a dropped connection does not stop them

Install tmux with sudo apt install tmux or sudo dnf install tmux, start a named session with tmux new -s work and run your command inside it. Press Ctrl+b then d to detach, log out, and later return with tmux attach -t work. The job keeps running on the server the whole time, even when your laptop sleeps or the network drops.

Topic: Server basics · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Why use tmux on a server?

Everything you start in a plain SSH shell is tied to that connection. When Wi-Fi drops or you close the lid, the shell receives a hangup signal and a running upgrade, database import, blockchain sync or compile stops halfway. tmux is a terminal multiplexer: it runs a small server process on the VPS that owns your shells, and your SSH login is only a viewer attached to it. Lose the viewer and the shells carry on.

The same tool also gives you several windows and split panes inside one SSH connection, so you can watch a log in one corner while editing a file in another. It is a standard package on every Linux image we offer and needs only a few megabytes of RAM. You will want it the first time you run a long task such as a release upgrade.

How do I install tmux and check the version?

tmux sits in the default repositories, so one command is enough:

# Ubuntu and Debian
sudo apt update && sudo apt install tmux
# Rocky Linux and AlmaLinux
sudo dnf install tmux
tmux -V

Ubuntu 24.04 and Debian 13 provide tmux 3.4 or newer, Debian 12 ships 3.3a and Rocky and Alma 9 carry 3.2a. Everything in this guide works on all of them. Install it as your normal sudo user, then start sessions as that same user; sessions belong to the account that created them.

How do I start, leave and return to a session?

Give each session a short name so you can find it again:

tmux new -s work
# ...run your commands...
# press Ctrl+b, release, then press d to detach
tmux ls
tmux attach -t work

All tmux key bindings start with the prefix, Ctrl+b by default. You press and release the prefix, then press the command key. After detaching you are back in the plain shell and can log out safely. tmux ls lists running sessions with their window count and creation time. If only one session exists, tmux a attaches to it without a name.

To end a session for good, exit every shell inside it, or run tmux kill-session -t work from outside.

How do I start a job that runs detached from the beginning?

For scripts you do not need to watch, create the session in the background and pass the command directly:

tmux new -d -s sync "rsync -a /srv/data/ deploy@203.0.113.10:/backup/data/"
tmux ls
tmux attach -t sync

When the command finishes, the window closes and the session ends with it. To keep the final output visible, end the command with a shell, for example "./import.sh; exec bash". You can also send keystrokes to a running session from a script with tmux send-keys -t sync "uptime" Enter.

For jobs that must run on a schedule or restart on failure, a proper systemd service or a timer is the better tool. tmux is ideal for interactive and one-off work.

How do windows and panes work?

A session holds windows, which are like tabs, and each window can be split into panes. The most useful keys, each pressed after the prefix:

  • c creates a new window, n and p move to the next or previous one, and a digit jumps to that window number.
  • , renames the current window, and w shows an interactive list of all windows.
  • % splits the pane left and right, " splits it top and bottom.
  • Arrow keys move between panes, z zooms one pane to full size and back.
  • x closes the current pane after confirming.

A practical layout: one window for an editor, a second split into a shell and journalctl -f, and a third running htop. Press Ctrl+b ? at any time to see every binding.

How do I scroll back and copy text?

Your terminal scrollbar does not see tmux history, so use copy mode. Press the prefix then [, move with the arrow keys or Page Up and Page Down, and press q to leave. To search upward, press ? inside copy mode and type a word.

For selecting text, start a selection with Space, move to the end and press Enter to copy it into the tmux buffer, then paste with the prefix followed by ]. With mouse mode enabled (next section) you can also scroll with the wheel, and holding Shift while dragging in most terminal programs selects text for your desktop clipboard.

What should go in a starter config file?

tmux reads ~/.tmux.conf on start. A small file fixes the most common annoyances:

set -g mouse on
set -g history-limit 50000
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on
set -g default-terminal "tmux-256color"
bind r source-file ~/.tmux.conf \; display "config reloaded"

This turns on the mouse for scrolling and pane selection, keeps a long scrollback, numbers windows from 1 to match the keyboard, and adds prefix r to reload. Apply changes to running sessions with tmux source-file ~/.tmux.conf. Some people move the prefix to Ctrl+a with set -g prefix C-a, unbind C-b and bind C-a send-prefix; that is purely a matter of taste.

What survives and what does not?

A tmux session survives a dropped SSH link and a normal logout. On Ubuntu, Debian, Rocky and Alma the login manager does not kill user processes on logout by default, so detached sessions keep running. They do not survive a reboot, because the tmux server is an ordinary process. Using the Restart or Stop action in the console, or a kernel update that needs a reboot, ends every session.

So before a planned reboot, check tmux ls and finish or stop jobs inside it. Plugins such as tmux-resurrect can save and restore window layouts, but not the state of running programs. The reboot guide lists other checks worth doing first.

How do I fix common tmux problems?

The message sessions should be nested with care means you are already inside tmux, often because you connected over SSH from a tmux pane on your laptop. Either detach locally first, or press the prefix twice to send it to the inner session. If colors look wrong, set default-terminal as shown above and make sure your local terminal reports 256 colors.

no server running from tmux ls simply means there are no sessions for your user; perhaps you created them as root. Run sudo tmux ls to check. A stale socket after an unclean stop can be cleared by removing /tmp/tmux-$(id -u) once no tmux process is running.

Questions about Keep sessions alive with tmux

Is screen still an option?

GNU screen does the same core job and is still packaged on Ubuntu and Debian. Rocky and Alma 9 dropped it from the base repositories, which is one reason tmux is the safer habit.

Can two people share one session?

Yes. If both log in as the same user, each can run tmux attach -t work and see the same screen at once. For different users you need a shared socket with group permissions.

Does tmux use much memory?

Very little. Each session costs a few megabytes plus whatever the programs inside it use. Large scrollback limits add some memory per pane.

Will tmux keep my job running if the server reboots?

No. tmux lives in memory, so a reboot ends all sessions. Use a systemd service for anything that must start again after a reboot.

How do I know whether I am inside tmux?

A green status bar at the bottom is the visual sign. From a script, check that the TMUX environment variable is set with echo $TMUX.