Introduction to tmux - How to run multiple terminal sessions and long running processes on a remote server

Looking to run an intense grep on your remote server? But now, you also need to compile node from source. Then, there's the fear of your connection dropping and having to redo all of this from scratch. tmux is the way out of this potential mess.

Introduction to tmux - How to run multiple terminal sessions and long running processes on a remote server
Photo by Lukas / Unsplash

Why?

tmux (Terminal MUltipleXer) is the goto utility when you need to run multiple commands, sessions on a remote server. You spawn new sessions, switch between them, disconnect and reconnect to your remote - and it's all still there!

Installation

sudo apt update
sudo apt install tmux

Basic Usage

After installation, tmux is immediately available for use. Launch a new session by running.

tmux

What this does, is launch a new tmux session, (unnamed, by default). The terminal you see, can be used to run any command, as usual. For example, you may need to run your node app for short time, before deploying into production. While still in the tmux session, run this (or whatever app you may want to run!)

npm run dev

What you now have, is your node app, running on the remote server, with the process and logs visible to you. Ordinarily, if you end your SSH session, or lose connectivity, your process would likely end. Not with tmux sessions. Let's see how. First, 'disconnect' out of the current session, by running the following command. Note, Ctrl+B, is the prefix here

ctrl+b, ctrl+d

➜  ~ tmux
[detached (from session 0)]


➜  ~ tmux ls
0: 1 windows (created Sun Jun  9 11:11:23 2022)

Unclear? Firstly, running tmux launched us into a new session. Then, the node app was started. Lastly, we disconnected from that session by hitting the shortcut ctrl+b, ctrl+d

To get a list of running sessions (because, we can have multiple such sessions running at any given time), use tmux ls. Just one session, is what we saw above.

Let's launch a few more sessions. And disconnect from them using the ctrl+b, ctrl+d shortcuts.  

➜  ~ tmux new -s backup-job
[detached (from session backup-job)]
➜  ~ tmux new -s compile-node
[detached (from session compile-node)]

Ok. Let's see what tmux has going on in the background. Run the tmux ls command again.

➜  ~ tmux ls
0: 1 windows (created Sun Jun  9 11:11:23 2022)
backup-job: 1 windows (created Sun Jun  9 11:18:59 2022)
compile-node: 1 windows (created Sun Jun  9 11:19:08 2022)

We have 3 sessions running, the first, is shown as 0, because it is an unnamed session. Noticed the '-s backup-job' addition to the session creation syntax? The -s flag allows us to name the session while creating it. The obvious benefit being, you can pick and restore sessions using this name.

That's it for this starter guide on tmux usage!

List of tmux commands

tmux    # launch a new session
tmux -s my_session    # specify session name and launch

tmux ls    # list running sesseions

tmux attach -t my_session    # attach to a running session
tmux a -t my_session    # attach to a running session, shorter form

tmux kill-session -t my_session        # kill a named running session

List of tmux shortcuts

Ctrl+b, Ctrl+d    # disconnect from session
Ctrl+b, x    # exit and kill current session
Ctrl+b, ,    # rename current window