I personally believe that using a terminal multiplexer is a must for anyone who spends a non-trivial amount working on remote *nix servers. This post is a gentle introduction to GNU screen but I would like to take a short detour and explain why they are useful.
Why use a terminal multiplexer?
Some of the most obvious advantages being:
- Ability to have a saved session for your work
- Cutting down the number of terminal windows you have to open
Let’s discuss a usecase wherein you are logged on to your Edge/Gateway node and would like to monitor your Spark jobs, your HBase service and at the same time query Impala tables. A common solution would be one of:
- Use a terminal emulator like Putty to log on to the edge node and use Spark/Hbase/Impala by switching directories and managing background processes OR
- Open three Putty windows and manage one service per window
This kind-of works but has a few problems:
- If you now want to log on to multiple servers, it greatly increases the number of windows you have to have open
- In case you lose connection, you are back to square one since you have lost all your sessions you were working on
Enter terminal multiplexers like screen and tmux: they allow you to manage multiple “virtual” windows within the same terminal emulator application and at the same time provide a mechanism to “save” your session. These sessions stick around till the server is restarted (which happens rarely).
tmux and screen
screen are the most widely used terminal multiplexers. I’m a big fan of tmux but it is almost never installed by default on the “enterprise grade linux installations” (RHEL) and it’s a pain to get additional software installed. I feel that screen is “good enough” for what it does and has the added advantage of being installed by default so I’ll be discussing screen in this post.
Basic screen commands
Below is my version of the very basic set of commands required to get started with
screen and the ones which I most frequently use. The convention followed is
C-a which means
Command) key followed by the character
- Fire up console and type the command
screen -U -S session_name. This will create a new screen session which supports
utf8and has a name
C-a cto create a screen window
C-a kto kill the screen window
C-a Ato rename a screen window to something logical. A title “spark monitoring” makes more sense than the default title “bash”!
C-a dto detach the screen session in case are about to restart your local box. The good news is that when you lose network connection, screen automatically “detaches” your session; neat!
C-a "(that’s a double quote) to list all the sessions in an interactive way which can be selected using
C-a numto jump to the numbered session. For example, let’s say you want to jump to jump to the second screen window, you will press
- Given that screen windows are different from your terimanal emulator windows (with its own buffer), use
C-a ESCkey sequence to drop in the “copy mode” wherein you can move around freely using
- To list all the active
screen -ls. To attach to the first session, use
screen -R. To attach to a named session (as retrieved from the
screen -lscommand) type
screen -r session_name.
- If you want to kill a screen session, use
screen -S session_id_or_name -X quitwhere
session_id_or_nameis the one which you get from