A practical look at the Screen terminal multiplexer

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Alexey Stiop, 123RF.com

Alexey Stiop, 123RF.com

Split Screens

With Screen, you can easily manage multiple terminal windows in a single session. This terminal multiplexer provides numerous amenities that allow admins to perform their daily tasks without a graphical environment.

Even in times when administrator desktops include powerful processors and large monitors with great graphical interfaces, servers in datacenters and such still need to be managed through a command line. Upgrades, new installations, program development, and monitoring – many things often happen all at once. Admins sometimes have multiple terminal sessions open to access a multitude of servers. With many open windows, you risk losing track of things.

Terminal multiplexers like Tmux and Screen provide some order to this potential chaos. They allow, among other things, multiple terminal windows in one session or parking and recalling sessions without interrupting any of the running processes.

This approach can also help when network connections go down. Admins can also share sessions with their colleagues, which can often help solve tricky problems more quickly.

In this article, I'll focus on the veteran multiplexer GNU Screen [1]. Screen was developed in 1987 by Oliver Laumann at the Berlin Technical University under the name BSD Screen and published in the net.sources user group. Juergen Weigert and Michael Schroeder of the University of Erlangen took over development in the years between 1993 and 2002. The baton passed to Wayne Davison in 2004 before the project sank into a multi-year hibernation with very minor changes made. At the beginning of 2014, however, the current maintainer Amadeusz Slawinski published a new version of Screen [2].

Screen provides much more than just continuous running and recall of processes in a terminal window and collaborative access to them. Multiple terminal windows can be opened, keystrokes logged, and content shared during a screen session. If a longer running process exists in a terminal, Screen can provide an alert when changes occur on the command line. Screen "windows" can also be split, so that you can view multiple sessions simultaneously.

Screen in Use

After installing the terminal multiplexer, you start it by typing the screen command. Depending on the distribution, Screen loads an info screen or drops you into a terminal window. In the latter case, you enter commands as usual – so far, so good. The only advantage up to now is that you can recall the session in case of a connection failure. However, Screen can do a lot more.

When working with Screen, you get around by using certain commands and keyboard shortcuts. Screen uses the key combination Ctrl+A as a control signal for further commands. Entering Ctrl+A and then pressing C, for example, opens a new window. Ctrl+A and Shift+2 lists all the windows and allows a change among them (Figure 1).

Figure 1: Individual windows can be controlled from a list. There is a more direct way, though.

Alternatively, you can use the screen number from the list and enter Ctrl+A and then <number>, where <number> is from 0 through 9. Using Ctrl+A and ? shows an overview of the key combinations. Ctrl+A and then K closes the active window. Closing all of them shuts down the Screen session. In the unusual case where you need more than 10 terminal windows, you can use Ctrl+A and then Shift+#+<number> to switch to a window with a double-digit number. However, this action requires compiling Screen with a higher MAXWIN number. Alternatively, you can move forward and backward through windows with Ctrl+A and N and Ctrl+A and P, respectively. Use Ctrl+A and then W to find out what window you're currently in.

If you're skipping between multiple Screen windows, you'll soon lose track, because the Bash shell names every window unhelpfully as bash . You also can't tell where a process is running, although you might recall what's behind each window number.

To solve this, you can give each window a different name with Ctrl+A and then Shift+A. Then, you can tell whether an update is running in window 7 or window 8, and that the config file with Vim is being processed in window 5. If you mistakenly use a command in a window, you can cancel it with Alt+Q.

Murphy's Law dictates that you'll underestimate the process duration just when you're about to leave for the day. Such processes include a full network scan or a complete LaTeX installation. If you've started such a process with all foresight in a Screen session, you can decouple from the session with Ctrl+A and then press D. Later, you can log in to the server again over SSH and open the session with

screen -r

If you're running multiple sessions, Screen will show you a list of running sessions with the associated PID. Use the Screen PID to open the screen you want to see. Alternatively, use the -S <name> parameter when opening the Screen session. Switch between sessions with Ctrl+A and Shift+. to the Screen command line and name the session with sessionname <name> .

You can also have the Screen inform you about the status of a running process. If you're downloading large files, migrating servers or compiling large programs, attach a screen with Ctrl+A and Shift+M, and use Ctrl+A and Shift+- to give you a quick status when Screen detects activity or a recently closed job in a window.

Alternatively, you can divide the Screen window into multiple regions to maintain better oversight of running processes (Figure 2). Older Screen versions allow only a horizontal division, which you activate using Ctrl+A and then Shift+S. The newest version also allows vertical division, and you can open a two windows side by side with Ctrl+A and then Alt+<. Screen calls the subwindows "regions," and you use Ctrl+A and Tab to switch from one to the other. Close the currently active region using Ctrl+A and then Shift+X.

Figure 2: Multiple windows are called regions, which you can divide horizontally or vertically.

During breaks or interruptions, you can lock the session using Ctrl+A and X. Note especially that a locked session doesn't prevent another user from hacking in from somewhere else. The session is still connected, and another user can run screen -r -D to get to it, whereas -D means detach. An unlocked session can be grabbed in an instant.

Log, Copy, and Paste

Some tasks need to be well documented. Admins often copy commands and other output to a text file. Screen makes this easier in that the program can log all input and output if you want. You activate logging with Ctrl+A and then Shift+H. Afterwards, you can find the log in the screenlog.# , where the hash is the number of the terminal window in the Screen session (Figure 3).

Figure 3: Screen can log window activities diligently so processes can be tracked and easily documented.

Screen also lets you copy text into a window. Use Ctrl+A and then Alt+8 or Ctrl+A and Esc to enable copy mode, then use the arrow keys or H, J, K, or L in Vim mode to move through the text. From the start position, use the space bar to mark the content to copy (Figure 4). Another press of the space bar copies everything into the text buffer, which you then copy using Ctrl+A and then 9.

Figure 4: If logging is not enabled, you can still copy important passages using copy and paste for easy documentation. You can copy the content into the different windows of a Screen session.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content