tmux 3.8 sessions

tmux attach, detach, list and kill: the long-job workflow

Start a named session with tmux new -s train, press Ctrl-b d to detach and log out, then run tmux attach -t train to come back and tmux kill-session -t train when you are done. Tested on tmux 3.8 and 3.5a. If tmux dies at logout, systemd-logind's KillUserProcesses=yes is the usual cause.

5 min readTerminals and remote servers

At a glance

  • tmux new -s train starts a named session; Ctrl-b d detaches and the job keeps running.
  • tmux ls lists sessions; tmux attach -t train puts you back inside.
  • tmux kill-session -t train ends one session, tmux kill-server ends all of them.
  • A session does not survive a reboot, and on systemd machines with KillUserProcesses=yes it 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:

Bash
# 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' Enter

Detach, 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:

Terminal
$ tmux ls
train: 1 windows (created Thu Oct 29 23:54:33 2026)
$ tmux attach -t train

tmux 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.

TaskCommandNotes
New named sessiontmux new -s trainCreates and attaches; add -d to stay outside
New detached session running a commandtmux new -d -s train 'cmd'The session ends when the command exits
Attach or createtmux new -A -s trainAttaches if train exists, creates it otherwise
List sessionstmux ls(attached) marks sessions with a client
Attachtmux attach -t traintmux a -t train also works
Attach and detach other clientstmux attach -d -t trainTakes the session over from another terminal
Attach read-onlytmux attach -r -t trainWatch without sending keys
DetachCtrl-b dThe session keeps running after you log out
Renametmux rename-session -t old newWorks while the job runs
Kill one sessiontmux kill-session -t trainTyping exit in its last shell does the same
Kill all other sessionstmux kill-session -a -t trainKeeps only train
Kill the servertmux kill-serverEnds 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:

Bash
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 train

In 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.

Bash
# 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.

Get a live linkpip install tqdm-live

Sources