Start a two-hour database import over SSH, lose the Wi-Fi, and the import dies with the connection. That is because a process started in a terminal belongs to that terminal; when the terminal goes, so does the process. A terminal multiplexer — screen or tmux — keeps a session running on the server independently of your connection. You attach to it, do work, detach, and come back later from anywhere.
tmux
The modern choice, installed on most images or one command away:
sudo apt install tmux -y # dnf install tmuxStart a named session:
tmux new -s importYou are now inside tmux; the terminal looks the same with a green bar at the bottom. Run your long command. Then detach with Ctrl+b then d. The session keeps running. Close your laptop, go home, reconnect:
tmux ls # list sessions
tmux attach -t import # back in, exactly where you left itIf the SSH connection drops instead of you detaching, the session is still there; tmux attach reclaims it.
The keys worth knowing, all after the Ctrl+b prefix:
| Key | Does |
|---|---|
d |
detach |
c |
new window |
n / p |
next / previous window |
% |
split pane left/right |
" |
split pane top/bottom |
| arrows | move between panes |
[ |
scroll mode (q to leave) |
x |
close pane |
Two panes — a command running on the left, htop or a log tail on the right — is the setup most people settle into.
screen
Older, everywhere, same idea:
screen -S import # start named session
# ... run the command ...
# Ctrl+a then d to detach
screen -ls # list
screen -r import # reattachPrefix is Ctrl+a; c new window, n/p to switch, [ to scroll. If a session shows as Attached after a dropped connection, screen -rd import detaches it elsewhere and attaches here.
nohup, for one-off jobs
If you just want one command to survive logout and do not need to interact with it:
nohup ./long-job.sh > long-job.log 2>&1 &Output goes to the file; & puts it in the background; nohup ignores the hangup signal when you log out. Check on it with tail -f long-job.log. You cannot reattach to it, which is why tmux is usually better for anything you might want to look at.
Things that should not be in tmux
A web app, a queue worker, a bot — anything meant to run permanently — belongs in a systemd unit, which restarts it on crash and on reboot; a tmux session does neither. Running your own app as a service is twelve lines. tmux is for interactive work and one-off long jobs.
Useful habits
- Name sessions for what they are doing (
tmux new -s migrate-shop);tmux lsa week later then makes sense. - Start tmux first, before the long command, not after remembering you should have.
- Log the output of anything important inside the session too (
command | tee job.log); tmux's scrollback is limited. - Reboots end sessions. A server restart kills every tmux session; anything that must survive a reboot is, again, a systemd job.
Every VPSPioneer managed VPS has tmux installed, and our own migrations and imports run inside it — the "SSH dropped during the database import" story is one we would rather not have.