How to Use screen and tmux to Keep Tasks Running After Logout

Run a long command on a server that survives a dropped SSH connection: screen and tmux basics, detaching and reattaching, and nohup for one-off jobs.

Published
Reading time
2 min

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:

bash
sudo apt install tmux -y     # dnf install tmux

Start a named session:

bash
tmux new -s import

You 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:

bash
tmux ls                 # list sessions
tmux attach -t import   # back in, exactly where you left it

If 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:

bash
screen -S import        # start named session
# ... run the command ...
# Ctrl+a then d to detach
screen -ls              # list
screen -r import        # reattach

Prefix 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:

bash
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 ls a 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.

#linux#tmux#screen#ssh#sysadmin

Keep reading

More from Linux

All guides

Linux

How to Manage Users and Groups on Linux

Create, modify, lock and remove user accounts, use groups to share access safely, set up a deploy user for a site, and audit who can log in to a server.

3 min read →

Linux

How to Set Up SSH Keys and Turn Off Password Login

Generate an ed25519 key on Mac, Linux or Windows, install it on the server, test it, and disable password authentication so brute-force attacks cannot succeed.

3 min read →

Linux

How to Create a Sudo User and Disable Root SSH Login

Create a user with sudo rights on Ubuntu, Debian, AlmaLinux or Rocky, test it, then lock root out of SSH without locking yourself out.

3 min read →