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 and tmux. 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. Note that 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, either through OnDemand or by ssh (e.g ssh abc123@monsoon.hpc.nau.edu or ssh 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 Ctrl+B; D for tmux or Ctrl+A; D 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 Ctrl+B 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 Ctrl+A 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, 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 & date. You can disconnect from your current session by pressing Ctrl+B, followed by the D key. When you have multiple sessions and want to switch between them, you can use Ctrl+B followed by a left parenthesis ‘(‘ to switch to the previous session. Likewise, you can switch to the next one using Ctrl+B and ‘).’
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:
- Ctrl+b; ‘$’ (dollar sign): Rename the currently attached session.
- Ctrl+b; ‘d’: Detach from your current session.
- Ctrl+b; ‘s’: List all sessions currently running.
- Ctrl+b; ‘(‘ (left parenthesis): Switch to previous session.
- Ctrl+b; ‘)’ (right parenthesis): Switch to next session.
- Ctrl+b; ‘?’ (question mark): List all keyboard shortcuts.
Additional Documentation
There is much more to tmux than described here. Other resources on using tmux are linked below.
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 <PID>.
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:
- Ctrl+a; ‘c’: Create a new screen session.
- Ctrl+a; ‘a’: Set the name of the current session.
- Ctrl+a; ‘d’: Detach from the current session.
- Ctrl+a; ‘?’ (question mark): Show help information.
Additional Documentation
Linked are further resources on using screen.