tmux – virtual consoles, background jobs and the like

In this section, we will be looking at another great tool called tmux. Tmux comes in particularly handy when working in remote ssh sessions, because it gives you the ability to continue your work from where you left off. It can also replace some of the features in terminator, if you are working, for example, on Mac, and you can't install terminator.

To get started with tmux on Ubuntu, we first need to install it:

sudo apt install tmux
tmux – virtual consoles, background jobs and the like

Then just run the command:

tmux
tmux – virtual consoles, background jobs and the like

And you will find yourself inside a brand new virtual console:

tmux – virtual consoles, background jobs and the like

For demonstration purposes, we will open up a new tab that you can see the list of open sessions with tmux ls:

tmux – virtual consoles, background jobs and the like

Let's start a new tmux named session:

tmux new -s mysession
tmux – virtual consoles, background jobs and the like

Here we can see that opening a tmux session maintains the current directory. To list and switch tmux sessions inside tmux, hit Ctrl + B S.

We can see that we can switch to another tmux session, execute commands inside, and switch back to our initial session if we want to. To detach (leave a session running and go back to the normal terminal) hit Ctrl + b d;

Now we can see we have two opened sessions.

To attach to a session:

tmux a -t mysession
tmux – virtual consoles, background jobs and the like

This scenario comes in handy when you login to a remote server and want to execute a long running task, then leave and come back when it ends. We will replicate this scenario with a quick script called infinity.sh. We will execute it. It's writing to the standard output. Now let's detach from tmux.

If we look at the script, it's just a simple while loop that goes on forever, printing text each second.

Now when we come back to our session, we can see the script was running while we were detached from the session and it's still outputting data to the console. I will manually stop it by hitting Ctrl + c.

Alright, let's go to our first tmux session and close it. In order to manually kill a running tmux session, use:

tmux kill-session -t mysession
tmux – virtual consoles, background jobs and the like

This will kill the running session. If we switch over to our second tab, we can see that we have been logged off tmux. Let's also close this terminator tab, and open a brand new tmux session:

tmux – virtual consoles, background jobs and the like

Tmux gives you the possibility to split the screen, just like terminator, horizontally with Ctrl + b + ", and vertically with Ctrl + b + %. After that, use Ctrl + b + arrows to navigate between the panes:

tmux – virtual consoles, background jobs and the like

You also have the possibility to create windows (tabs):

These last functionalities are very similar to what terminator offers.

You can use tmux in situations where you want to have two or more panes or even tabs in your remote ssh connection, but you don't want to open multiple ssh sessions. You could also use it locally, as a terminator replacement, but the keyboard shortcuts are much harder to use. Although they can be changed, you will lose the option to use tmux remotely, because opening a tmux session in another tmux session is discouraged. In addition, configuring new tmux keyboard shortcuts might make tmux a burden when working on lots of servers due to the shortcut differences.