Tmux
What is tmux
tmux is a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command line interface, and for separating programs from the Unix shell that started the program. It is a rewrite of GNU Screen under a BSD license.
Using tmux
for long-running jobs
Often we find ourselves running a job (non-graphical) that takes a lot of time to complete. Of course we initiated this job by SSH'ing from our laptop to some departmental system and we now want to leave but if we do, the job will terminate. Tmux to the rescue!
When you start tmux, all processes running inside it are referred to as a session. Tmux has the friendly ability to detach and re-attach sessions leaving the processing running. This is great for running long-ish jobs that you want to keep an eye on or to keep your processes alive if you're using a flaky internet connection.
Please keep in mind that tmux is not a replacement for IT:Resource control systems like Torque, Slurm or SGE.
To use tmux for session management, all you have to do is the following:
Start tmux
[ambrish@zephyr ~]$ tmux
You will be presented with a terminal that looks like this:You may then start your long-running job
[ambrish@zephyr ~]$ for i in `seq 500`; do
> echo $i
> sleep 2
> done
1
2
3
4
5
6
It is then possible to detach your tmux session by typing
Ctrl-b d
(press control and b at the same time, release and type d). You will then be presented with the following:
We may then disconnect the SSH connection, and go home knowing that tmux will keep your job running.
When you return, you may log into the computer again and type tmux attach to re-attach your tmux session. Notice how the job continued while you were home sleeping:
for terminal management
We've just seen how we can use tmux for attaching and detaching a single process. Now we'll look into how we can deal with multi-processes in tmux.
We will be using the default configuration for tmux which uses ctrl-b
as the leader for all commands. We will often use syntax ctrl-b $K
which means press control and b together, release and hit key $K.
with multiple windows (tabs)
By default, when we start tmux we are presented with a single window as seen here:
If we wish to open another window inside tmux, we may issue ctrl-b c
. We will then be presented with the following screen:
We now have two windows (tabs) denoted at the bottom in green as 0:bash 1:bash- 2:bash*
. The * indicates the active window. To switch between windows, we may:
ctrl-b n
to go to the next windowctrl-b p
to go to the previous window
We may also rename these windows using ctrl-b ,
.
Please note that if we detach the tmux session, we detach all windows in the session. We may reattach the entire session with all windows later.
with multiple panes
It is often convenient to split a window into multiple panes. To do this, we may:
ctrl-b "
to split the window horizontally into two panes.ctrl-b %
to split the window vertically into two panes.
Then to navigate between the panes, we may:
ctrl-b o
to navigate to an **o**ther pane.ctrl-b ;
to navigate to an other pane in the reverse direction.ctrl-b q
Shows pane numbers. Type the pane number will direct you to that pane.
In addition, we may manipulate these panes via:
ctrl-b {
to swap panes.ctrl-b <space>
to auto-arrange panes.ctrl-b :break-pane
Breaks the pane to form its own window.
A neat trick we can perform when we have multiple panes open is to ctrl-b :setw synchronize-panes
. This will allow you to type in one pane, and have your keyboard entry be reflected to all panes in a window. When we are done issuing commands to all panes, we can get out of this mode my re-typing the command ctrl-b :setw synchronize-panes
.