minimal example of a “detached” script with tmux

Photo by Adi Goldstein / Unsplash

This is a minimal example of how you would run a script “detached” on a remote server, so you can disconnect SSH, and free up your Terminal to do other things, or even close your Terminal and turn off your computer altogether.

Let’s say we want to run some script on a server, say, that would run for 1 hour. We wish to have the script running without needing to be connected to it on our computer, and waiting for it to finish.

The script is quite plain and simple, so we are confident that it won’t take down the server, thus we can leave it running. (Perhaps the server is not even important enough for us to worry about it anyway, like a testing server.)

enter tmux

We use tmux (terminal multiplexer) to start a “session” on the server. Inside the session, we can run scripts as we normally do. The tmux session is just a standard command line interface with extra stuff. When we detach the session, the script will continue to run inside the session on the server. When we log off, it won’t terminate the script, which is still running in the tmux session. At a later time, we can come back and “reattach” the session.

First, we ssh into the server.

ssh username@example.example

check and install tmux

We need to ensure that tmux is installed on the server. We only need to do this once.

It should display the version number if tmux is installed:

$ tmux -V
tmux 1.8

Otherwise, it should display “command not found”:

$ tmux -V
bash: tmux: command not found...

To install tmux if it is not already installed on your server:

# fedora
sudo dnf install tmux -y

# rhel
sudo yum install tmux -y

# macos
brew install tmux

# debian
sudo apt install tmux -y

running tmux

To start a new session, simply run the tmux, we would see a green bar on the bottom of the tmux session.

Now we can run any command as we normally would.

Let’s run our script, then detach the session.


detaching tmux

To detach, use the following keyboard shortcuts:

  1. Press both keys: CTRL and b.
  2. Release both keys.
  3. Press d.

Think of CTRL-b as a way of telling tmux that a command is coming, and d is for “d” in “detach”.

We would see that tmux’s green bar has disappeared. The script would continue to run in the tmux session. We can now disconnect from SSH and the script will continue to run.

reattaching tmux

We can re-attach the session any time we like to check the script and see if it has finished or is still running.

To do this we would ssh back to the server, then run:

tmux a

You should now see tmux’s green bar at the bottom again. The ‘a’ in the tmux a command stands for “attach”, which will just attach a detached session.

At this point, we will see if the script is still running. We can detach the session again if the script is still running, or if the script has finished, type exit to end the session.

If you have multiple sessions, you can use tmux ls to list them. If you run multiple sessions, you would give each session a name, so when you want to reattach them, you can specify the exact session by its name.

I would use 1 session for the purpose of leaving a script running on a server if my situation allows, so it is easy to detach and reattach sessions without worry about names. There are only 2 commands that I would need, tmux and tmux a.

tmux can do a lot more

You can certainly do a lot more with tmux: tabs, splitting panes, etc.

For all other powerful features of tmux, I would recommend the following guide: 😄

Show Comments