tmux – Run terminal commands and allow disconnection

This quick guide will show you how to run a command in an SSH session and allow you to disconnect and leave the command to keep running. I use this frequently for running my backups to external drives so that I can kick them off then shut down my laptop instead of leaving it running all night.

Usually if you kick off a command in an SSH session then disconnect, the command will also stop running. But using a tool called tmux , short for terminal multiplexer’, you can avoid this issue.

tmux is essentially another session which runs separately. You start a tmux session then run whatever you want in it. Then you can disconnect and come back to the session later. It is very similar to screen  but I found much easier to use.

It is very simple…

  • Make sure you have it installed: sudo apt-get install tmux
  • Start a tmux session: tmux
  • Run whatever you want…. e.g. start a backup job.
  • Disconnect from your session: Ctrl + b allows you to send tmux admin commands. So use Ctrl + b  then d  for ‘disconnect’. There are various other admin keys you can use but ‘d’ is all I’ve ever needed. I know you can split screens, and some other things too.
  • You can now quit your SSH session.
  • Once you come back to your computer, log in again to the SSH session then run tmux ls  to see any running tmux sessions.
  • Connect to your session again:  tmux attach -t 0  where -t stands for target and 0 is the session name displayed in your tmux ls  output.
  • You can exit a tmux session and end it at the same time with Ctrl + d
  • Or you can kill a running sessino from the SSH command line with tmux kill-session -t 0

The developer has a nice guide/description here:

And there is a nice cheat sheet for tmux commands here:

Leave a Reply

(email optional)