If an SSH connection is interrupted (the laptop goes to sleep, the Wi-Fi connection falters, or the terminal is closed), any commands running via that connection will also stop. However, an interrupted system update, database import or large file transfer can leave the server in an undesirable, or even damaging, state.
The solution is a terminal multiplexer, such as tmux or screen. These run a session on the server which remains active even after the connection is closed, and to which you can reconnect later (even from another machine).
tmux
Installation
# AlmaLinux, Rocky Linux
sudo dnf install tmux
# Ubuntu, Debian
sudo apt install tmux
Usage
# Új, elnevezett munkamenet indítása
tmux new -s frissites
You can work in the session that opens in exactly the same way as you would in a normal terminal. Start the long-running operation, then:
- Detach (the session continues to run in the background): press the
Ctrl+bkey combination, release it, then press thedkey. - You can then safely log out of SSH. If the connection is unexpectedly lost, this will also terminate the session.
Reconnecting, even from a new SSH connection:
# A futó munkamenetek listája
tmux ls
# Csatlakozás egy munkamenethez
tmux attach -t frissites
The session ends when you exit from within it using the exit command, or when it is terminated externally:
tmux kill-session -t frissites
Useful keyboard shortcuts
Every tmux command begins with the prefix Ctrl+b:
| Key | Action |
|---|---|
Ctrl+b d |
logging out of the session |
Ctrl+b c |
New window within the current session |
Ctrl+b n / p |
Next / Previous window |
Ctrl+b % |
Splitting the window into two adjacent sections |
Ctrl+b " |
Splitting the window into two sections, one below the other |
Ctrl+b, arrow keys |
switch between sections |
Ctrl+b [ |
Scrolling in the output (exit: q) |
screen
screen is an older equivalent of tmux, but is still widely used. From AlmaLinux and Rocky Linux 8 onwards, it is available in the EPEL repository (version 5.0.1 was used in the test on AlmaLinux 10; see: Installing the EPEL repository on AlmaLinux). On the test machines, both tmux and screen were pre-installed on the Ubuntu 24.04 cloud image; on Debian 12, screen was pre-installed; on AlmaLinux 10, neither was:
# AlmaLinux, Rocky Linux
sudo dnf install epel-release
sudo dnf install screen
# Ubuntu, Debian
sudo apt install screen
# Új, elnevezett munkamenet
screen -S frissites
# Leválás: Ctrl+a, majd d
# A futó munkamenetek listája
screen -ls
# Visszacsatlakozás
screen -r frissites
If the screen -r command shows that the session is still Attached because the old connection did not terminate properly, you can detach it and take it over using the screen -d -r frissites command.
nohup: a single command running in the background
If you do not need to see the command’s output, and simply want it to continue running even after the connection is closed, nohup is sufficient:
nohup ./mentes.sh > mentes.log 2>&1 &
The command runs in the background, and its output is written to the mentes.log file, which you can monitor using the tail -f mentes.log command. nohup is not suitable for interactive commands (those that prompt for input, such as an update); for these, use tmux or screen.
When should you use it?
We recommend that you run any operation that takes longer than a few minutes, or which would cause problems if interrupted, within a tmux or screen session:
- system update, particularly upgrading to a new major version (see: System update using dnf and apt),
- Importing or exporting a large database,
- Copying files between servers (see: Copying files between servers: scp, rsync and sftp),
- Creating or restoring a backup.