Running long jobs inside tmux
If you start a long command in an SSH session and the connection drops, the command usually dies with it. A terminal multiplexer keeps the shell alive on the server, independent of your connection.
The basic loop
tmux new -s work # start a named session
# ... run your long command ...
# press Ctrl-b, then d to detach
tmux ls # list sessions
tmux attach -t work # come back later
After detaching you can close the laptop. The job keeps running, and when you attach again you see everything it printed in the meantime.
A few habits
- Name sessions after the task.
tmux lsis much more useful with names likeimportandbuildthan with numbers. - To scroll back, press
Ctrl-bthen[, move with the arrow keys or Page Up, and pressqto leave scroll mode. - When the job finishes, type
exitto close the window so old sessions do not pile up.
Alternative
For a one-off command that needs no interaction, nohup command & with output redirected to a file is enough. I reach for tmux when I want to look at the job again.