# Terminal Multiplexers

## Introduction

A terminal multiplexer is a piece of software that sits between your terminal and your shell, allowing you to disconnect from, reconnect to, and switch between shell sessions at will. There are many situations in which this can be useful:

- If your connection fails, a terminal multiplexer can let you pick up where you left off.
- If you are running an interactive command that requires a lot of time to complete on a compute node, a multiplexer can allow you to access it without needing to be connected to the compute node for the duration of the command.
- If you disconnect and reconnect, a terminal multiplexer will allow you to keep your terminal history intact for scrolling.

There are a number of widely-used terminal multiplexers. Two of the more common ones are [`screen`](<https://www.gnu.org/software/screen/>) and [`tmux`](<https://github.com/tmux/tmux/wiki>). Each of these has their own benefits and drawbacks, and both are immediately usable when logging into Monsoon.

## Quick Start

If you just want to get started with a long-running process on Monsoon without learning more detailed information on `screen` or `tmux`, there is a straightforward process to do so.

> [!WARNING]
>
> You *must* run `tmux` or `screen` on a **login node** (*monsoon* or *rain*), not a compute node, or your session will simply disappear when your work concludes. Likewise, you should not run long-running processes on login nodes, those should be run on compute nodes.

- [Connect to Monsoon](<https://rcdata.nau.edu/hpcpub/docs-test/connecting-to-monsoon/index.html>), either through OnDemand or by ssh (e.g *ssh [abc123@monsoon.hpc.nau.edu](<mailto:abc123@monsoon.hpc.nau.edu>)* or *ssh [abc123@rain.hpc.nau.edu](<mailto:abc123@rain.hpc.nau.edu>)*, where you **replace abc123 with your NAU user ID**).
- Run `tmux` or `screen`. This will create a new session.
- Create a shell session on a compute node using `srun -–pty /bin/bash`, along with any other options you need to pass (e.g `--mem=2048`).
- Run any long-running commands you had planned inside the compute node shell session.
- Detach from the session using <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> <kbd class="key-d">D</kbd> for `tmux` or <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-a">A</kbd> <kbd class="key-d">D</kbd> for `screen`.
- Log out of your SSH session or OnDemand tab.
- When you are ready to return, log back into a login node.
- Reattach to your session by running `tmux a` for `tmux` or `screen -r` for `screen`.
- You will now once again be in your shell on the compute node.

## Unexpected Behavior

Terminal multiplexers can cause behavior which is not intuitive to someone used to directly interacting with a terminal. This can include:

- Terminal scrollback will not function the same when reattaching to a session.
- In `tmux`, you can re-enable terminal scrollback by pressing <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> and typing `:set mouse on`
- (`tmux`): Ctrl+B is used by `tmux` and therefore cannot be used by applications in the terminal.
- (`screen`): Likewise, `screen` overloads <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-a">A</kbd> with similar behavior.

## Using *tmux*

*tmux* is a free and open-source terminal multiplexer. It also includes support for displaying multiple terminals using windows and panes, as well as providing a system for copying and pasting text from terminal to terminal or to/from a text file.

### Session Management

Once you have [logged into Monsoon](<https://rcdata.nau.edu/hpcpub/docs-test/connecting-to-monsoon/index.html>), you can create a tmux session by using the `tmux`, `tmux new`, or `tmux new-session` commands. You can add a name to your session by using the `-s <name>` flag, like so: `tmux new -s my-session`.

After running one of these commands, you will be in a new `tmux` session. You can use the terminal as usual. There should be a footer present at the bottom of your terminal showing some information about the session, like its ID, name, the hostname of the system it is running on (e.g `rain` on Monsoon), and the current time &amp; date. You can disconnect from your current session by pressing <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd>, followed by <kbd class="key-d">D</kbd>. When you have multiple sessions and want to switch between them, you can use <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> followed by <kbd>(</kbd> to switch to the previous session. Likewise, you can switch to the next one using <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> and <kbd>)</kbd>.

You can reattach to your most recent session by using `tmux -a` or `tmux attach`. If you need to see a list of all current sessions, you can run `tmux ls` or `tmux list-sessions`. Once you are done with a session, you can delete it by running `tmux kill-session -t <session-name>`.

If you forget a keyboard shortcut, you can view a list of all the shortcuts using `tmux list-keys`.

### Keyboard Shortcuts

While many of these shortcuts have already been described, here is a list of useful shortcuts for `tmux`:

- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> <kbd>$</kbd>: Rename the currently attached session.
- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> <kbd>d</kbd>: Detach from your current session.
- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> <kbd>s</kbd>: List all sessions currently running.
- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> <kbd>(</kbd>: Switch to previous session.
- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> <kbd>)</kbd>: Switch to next session.
- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-b">B</kbd> <kbd>?</kbd>: List all keyboard shortcuts.

### Additional Documentation

There is much more to `tmux` than described here. Other resources on using `tmux` are linked below.

- [Red Hat: A Beginner's Guide to `tmux`](<https://www.redhat.com/en/blog/introduction-tmux-linux>)
- [`tmux` Official Wiki](<https://github.com/tmux/tmux/wiki>)

## GNU Screen

`screen` is a free and open source terminal multiplexer distributed by the GNU Foundation.

### Session Management

Creating an initial `screen` session is as simple as running `screen` with no options.

You can view all currently-running screen sessions using `screen -ls`. From there, you can reattach to the most recently-detached session using `screen -r`, but this only works if there is exactly one session running. If there is more than one and you want to specify which session to reattach to, you can use `screen -r <session-name>` or `screen -r`.

When initially creating a `screen` session, you can specify a name using `-S <session-name>,` e.g `screen -S mySession`. This name will appear in the results of `screen -ls` and can be used to reattach with, for example, `screen -r mySession`.

### Keyboard Shortcuts

The main keyboard shortcuts used with `screen` are as follows:

- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-a">A</kbd> <kbd class="key-c">C</kbd>: Create a new `screen` session.
- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-a">A</kbd> <kbd class="key-a">A</kbd>: Set the name of the current session.
- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-a">A</kbd> <kbd class="key-d">D</kbd>: Detach from the current session.
- <kbd class="key-control">Ctrl</kbd>\+<kbd class="key-a">A</kbd> ++?++: Show help information.

### Additional Documentation

Linked are further resources on using `screen`.

- [GNU Foundation: `Screen` User's Manual](<https://www.gnu.org/software/screen/manual/screen.html>)
