Software:Tmux: Difference between revisions

From Atmospheric and Oceanic Science
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
= What is 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.[1] It is a rewrite of GNU Screen under a BSD license.
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.


[[File:Tmux Example.png|thumb]]
[[File:Tmux Example.png|thumb]]
Line 15: Line 15:
To use tmux for session management, all you have to do is the following:
To use tmux for session management, all you have to do is the following:


  * Start tmux \\ <code>[mhavas@zephyr ~]$ tmux</code>You will be presented with a terminal that looks like this:
* Start tmux <br> <code>[ambrish@zephyr ~]$ tmux </code>You will be presented with a terminal that looks like this:


{{ :it:tmux-02.png?500 |}}
[[File:Tmux Start.png|thumb]]


  * You may then start your long-running job \\ <code>[mhavas@zephyr ~]$ for i in `seq 500`; do
* You may then start your long-running job <br>
> echo $i
<nowiki> [ambrish@zephyr ~]$ for i in `seq 500`; do
> sleep 2
> echo $i
> done
> sleep 2
1
> done
2
1
3
2
4
3
5
4
6
5
</code>
6
</nowiki>


  * 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
* It is then possible to detach your tmux session by typing <code>'''Ctrl-b, d'''</code> (press control and b at the same time, release and type d). You will then be presented with the following:
[[File:Tmux detached.png|thumb]]


{{ :it:tmux-03.png?500 |}}
* 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:
  * 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:


[[File:Tmux Reattached.png|thumb]]
[[File:Tmux Reattached.png|thumb]]

Revision as of 16:56, 27 August 2021

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:
Tmux Start.png
  • 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:
Tmux detached.png
  • 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:
Tmux Reattached.png


Using tmux 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.

Using tmux with multiple windows (tabs)

By default, when we start tmux we are presented with a single window as seen here:

It:tmux-02.png?500

If we wish to open another window inside tmux, we may issue **ctrl-b, c**. We will then be presented with the following screen:

It:tmux-05.png?500

We now have two windows (tabs) denoted at the bottom in green as 0:bash- 1:bash*. The * indicates the active window. To switch between windows, we may:

  * **ctrl-b, n** to go to the **n**ext window
  * **ctrl-b, p** to go to the **p**revious 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.


Using tmux 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.
!: Note that a window can be split indefinitely.

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**.