From HPC Wiki
Revision as of 11:13, 12 June 2020 by (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The tools screen and tmux are window managers. Their main purpose is to allow the user to perform multiple tasks in a Shell or during an ssh session at the same time. This is comparable to multiple tabs in a browser.


Screen and tmux allow to perform multiple tasks at once. An important concept is detaching and attaching: detaching means that you put a screen / tmux session into the background. You can later attach to any session which was detached (e.g. if you need the output of a command). Closed sessions cannot be attached to.

A huge advantage is that these commands decouple task and terminal: you can log into an ssh session, start screen (or tmux), and then start a long-running task (e.g. download a file via wget). Afterwards, one can detach the screen session and then exit the ssh session. The download is now still running in the background (without screen it would terminate when exiting the ssh session). This is even more helpful if the ssh connection is unstable or might get interrupted: using screen or tmux, the connection loss does not interrupt the tasks which are currently running in the remote terminal.

Using screen

If you want to start a new session, you will need to run:

$ screen

On the other hand, you can attach to existing sessions with:

$ screen -r [name]

where the name of the session is needed if more than one exist (running without a name will list the possible sessions).

During a session, you can create multiple windows. A window corresponds to a tab in a browser and is thus able to run a task independent and parallel. Furthermore, you can split the shell screen in multiple regions which you can see at the same time. In order to perform these commands, you first need to press Ctrl + a.

d detach the current screen session
c create a new window in the current screen session
space switch between the windows of the current screen session
S split the screen horizontally (note the capital letter!)
| split the screen vertically
tab switch between splitted regions
? show all keybindings (commands)

In contrast to windows, regions are not independent which is why you typically create a window for each region:

Ctrl + a + |
Ctrl + a + tab
Ctrl + a + c

which splits the screen, switches to the newly created screen region and then starts a new independent window in this region.

Using tmux

Similarly, you can start a new session with:

$ tmux

If you want to attach to the last visited session, you can run:

$ tmux a

Or if you want to attach to another session, run:

$ tmux ls
$ tmux a -t name

to first list all sessions and then attach to one of them.

In contrast to screen, you need to start all commands during a session by typing Ctrl + b. Another important difference is that when splitting the screen into different regions (which in tmux are called panes), these are automatically independent. Thus, a single tmux session consists of several independent windows and each window consists of several independent panes. Important commands are:

d detach the current tmux session
c create a new window in the current tmux session
0/1/.../9 switch to window 0/1/.../9 of the current tmux session
& kill current window
% split current window vertically
" split current window horizontally
Arrow switch current pane
x kill current pane
? show all keybindings (commands)

Note: tmux use $TMPDIR to save the communication socket. This directory may be temporarily and/or session-dependent in some environments; once removed you will lost your connection to (still running) tmux sessions. To solve that put your tmux session data to a path not beind cleaned away after logout, e.g.

$ tmux -S /tmp/$USER/tmux new

and afte re-login

$ tmux -S /tmp/$USER/tmux ls

GUI and X11

Despite the principally possibility to use screen with GUI applications, you do not want to do so. Instead use FastX or VNC

Further Reading