At a glance
tmux new -s trainstarts a named session;Ctrl-b ddetaches and the job keeps running.tmux lslists sessions;tmux attach -t trainputs you back inside.tmux kill-session -t trainends one session,tmux kill-serverends all of them.- A session does not survive a reboot, and on systemd machines with
KillUserProcesses=yesit does not survive logout either.
Start a named session for a long job
When you kick off a long build, backup, migration or training run over SSH, create a named session with tmux new -s train, which puts you straight into it. If you do not want to enter it right away, tmux new -d -s train creates it detached.
You can pass the job directly, as in tmux new -d -s train 'python -u train.py'. That runs the command as the session's only program: when it exits, the window closes and the session disappears, taking the final output with it. To keep the output, start the session with a shell and type the command into it:
# Start a session and attach to it
tmux new -s train
# Or create it detached and type the job into its shell
tmux new -d -s train
tmux send-keys -t train 'python -u train.py' EnterDetach, log out and come back
Once the job is running inside tmux, press Ctrl-b d (the prefix, then d) to detach. The session and everything in it keeps going on the server after you log out of SSH.
When you log back in, run tmux ls to see what is running. It prints one line per session and adds (attached) at the end when a client is connected to it:
$ tmux ls
train: 1 windows (created Thu Oct 29 23:54:33 2026)
$ tmux attach -t traintmux a -t train also works, because tmux accepts unambiguous command prefixes. If the session is still attached somewhere else, tmux attach -d -t train detaches every other client first, while tmux attach -r -t train attaches read-only without disturbing anything. For one command that attaches when train exists and creates it otherwise, use tmux new -A -s train.
Kill sessions you no longer need
tmux rename-session -t old new renames a running session. When the job is done and you have read the output, end the session by typing exit in its last shell, or from outside with tmux kill-session -t train.
To clean up leftovers at once, tmux kill-session -a -t train kills every session except train and tmux kill-server ends them all. With no sessions left, tmux prints no server running on /tmp/tmux-1000/default (the number is your user id); a wrong name gives can't find session: NAME.
| Task | Command | Notes |
|---|---|---|
| New named session | tmux new -s train | Creates and attaches; add -d to stay outside |
| New detached session running a command | tmux new -d -s train 'cmd' | The session ends when the command exits |
| Attach or create | tmux new -A -s train | Attaches if train exists, creates it otherwise |
| List sessions | tmux ls | (attached) marks sessions with a client |
| Attach | tmux attach -t train | tmux a -t train also works |
| Attach and detach other clients | tmux attach -d -t train | Takes the session over from another terminal |
| Attach read-only | tmux attach -r -t train | Watch without sending keys |
| Detach | Ctrl-b d | The session keeps running after you log out |
| Rename | tmux rename-session -t old new | Works while the job runs |
| Kill one session | tmux kill-session -t train | Typing exit in its last shell does the same |
| Kill all other sessions | tmux kill-session -a -t train | Keeps only train |
| Kill the server | tmux kill-server | Ends every session |
Every other key binding is in the tmux cheat sheet.
When tmux dies after you log out (systemd)
Sometimes you detach, log out and come back to find the session gone. The usual cause is the systemd-logind setting KillUserProcesses=, whose upstream default is yes. The logind.conf manual itself warns that this breaks tools like screen and tmux unless they are moved out of the session scope.
Many distributions build with no instead. Debian 13, for example, ships #KillUserProcesses=no in /etc/systemd/logind.conf. On a system where it is yes, logging out kills both tmux and your job.
Check the effective value and your lingering status, then start tmux outside your login session, as the systemd-run manual shows:
busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses
loginctl show-user $USER -p Linger
# Keep your user manager running after the last logout, then start tmux under it
loginctl enable-linger
systemd-run --scope --user tmux new -s trainIn the default configuration you can enable lingering for yourself without root. An admin can also set KillUserProcesses=no in /etc/systemd/logind.conf or list specific users in KillExcludeUsers=. Other ways to keep a job alive are in run a process in the background on Linux.
Check on a job without attaching
You do not have to attach just to see what your job is doing. tmux capture-pane -p -t train prints what is on the pane's screen; add -S -20 to start 20 lines back in the history, or -S - for all of it. For a continuous log file, tmux pipe-pane -o -t train 'cat >> ~/train.log' copies everything the pane prints from now on into ~/train.log, carriage returns included.
# Last 3 non-empty lines of the pane, from a script or a cron job
tmux capture-pane -p -t train | grep -v '^$' | tail -n 3
# Log everything the pane prints from now on
tmux pipe-pane -o -t train 'cat >> ~/train.log'Both commands still need an SSH session every time you want to look. Also pass environment variables with tmux new -e NAME=value, because a tmux server that is already running does not see variables you export later.
FAQ
How do I detach from tmux without killing the session?
Press Ctrl-b d, the prefix key and then d. The session and everything running inside it keeps running on the server after you log out.
How do I attach to a session that is still attached somewhere else?
Run tmux attach -d -t train to detach every other client before attaching your terminal. If you only want to watch without taking over, run tmux attach -r -t train to attach read-only.
Why does tmux say "no server running"?
When no sessions are left on the machine, tmux commands print no server running on /tmp/tmux-1000/default. Either you have not started a session yet, the session's command exited and closed it, or the server was stopped by tmux kill-server, by systemd-logind with KillUserProcesses=yes, or by a reboot.
Does a tmux session survive a reboot?
No. A reboot ends the tmux server and every process running inside it, so the job has to be started again.
How do I check from a script whether a session exists?
Run tmux has-session -t train. It exits with status 0 if the train session exists and 1 if it does not, so it works directly in an if statement.
Check on long runs from your phone
Change one import and every tqdm bar prints a link you can open on any device. Get a Discord or Slack message when the run finishes, fails or stalls.
pip install tqdm-liveSources
- tmux manual page man.openbsd.org
- tmux 3.8 release on GitHub github.com
- logind.conf manual: KillUserProcesses freedesktop.org
- systemd-run manual: starting screen as a user scope freedesktop.org
- loginctl manual: enable-linger freedesktop.org