Gnu Screen ; Different regions, Logging, Screen Window Types

I wrote this Gnu Screen guide in December ’08 with the previous incarnation of in mind. January 2009 however, started on its new and exciting path as a community site and the article just sat on my machine, waiting. So I decided to publish it on my blog in a four part series over the following month. Maybe someone can use it 😉

Have you ever had to run a time consuming process (say mkfs or compiling something) over a shoddy ssh connection? Or cleanly run several commands in parallel without having several open connections to that server? Or maybe you were programming and wanted a mysql prompt, a bash prompt and several text editors open? How about that ssh tunnel you need open and dont want to close by accident? And did you ever need to perform the same task on several different machines? Or connect to a serial terminal? Or maybe you just want a log file of what you re doing..
These are day to day situations any linux user will run into, that can be quite frustrating. That is, before you discover Gnu screen!

“Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.” Or at least, thats what the site calls it. Sounds easy enough, but there are an amazing amount of different uses for this incredible piece of software. The above list is some of the uses I could readily think about – some of the ways I regularly use screen, but like any decent linux tool – the sky really is the limit! I’ll be looking into different issues, so lets jump into some of the practical solutions for those problems.

The Fun Stuff – Different regions
Sometimes, having several windows is not enough.. It can help to have several different windows visible in your terminal and screen is able to provide you with that. As before, we need to open a screen session and open several windows. You could actually also do this with one window, but there wouldnt really be much point to it with one window. Once the new windows are opened, you can split the screen with the following shortcut.

C-a S       (split)       Split the current region into two new ones.
C-a tab     (focus)       Switch the input focus to the next region.

With these commands, you can create several regions and switch between them. These regions contain the same regular windows we spoke about before and as such, all normal commands can be used to control their contents. You can choose any window to be shown inside these regions. To close and further controll these regions, you can use the following shortcuts.

C-a X       (remove)      Kill the current region.
C-a Q       (only)        Delete all regions but the current one.
C-a F       (fit)         Resize the window to the current region size.

These are the default defined shortcuts for the regions. There are however more commands to control your regions, they just are not bound to shortcuts. For a comprehensive list of these other options, I advise you to comb trough the screen man page and do a search for “region”. There is ,however, one interesting command I don’t want you to miss! (Small blatant man copying coming up)

       Resize  the  current region. The space will be removed from or added to
       the region below or if there’s not enough space from the region  above.
              resize +N   increase current region height by N
              resize -N   decrease current region height by N
              resize  N   set current region height to N
              resize  =   make all windows equally high
              resize  max maximize current region height
              resize  min minimize current region height

You will probably not want these windows the same size, so the resize command is really indispensable. Just like before, to use this commands, you have to call up the command line mode and input these commands there.

The Fun Stuff – Logging
Screen provides a simple mechanism for logging a session. It suffices to enable the logging in any of the three ways described below. The Parameter

-L    turns output logging on for this window.

The shortcut

C-a H       (log)         Begins/ends logging of the current window to the file "screenlog.n".

Or the command.

log [on|off]

These commands enable logging to the standard “screenlog.%n” file, in the standard directory. Should this file already exist, screen will append the new data to the existing file.
To configure the logging in more detail, screen also accepts the following two commands.

logfile filename
logfile flush secs

These commands allow the user to configure what name the logfile should have and how many seconds screen should wait before flushing the the logfile buffer to the system.

  • Or maybe you just want a log file of what you re doing..

And using the above command, keeping that logfile is as easy as adding a parameter or using a shortcut. In using this system, you will soon discover that the logging is a wonderful and valuable tool, but also that when it comes to color tags and advanced ascii based menus or effects, the logs might turn out less than optimal. I’m thinking about the SLES Yast tool or even the fancy scp or yum progress bar. These commands will “replay” trough the log files while you read them and this does not always look as nice as you d probably like it to. This is however unavoidable and as such quite normal.
You might also want to look into the hardcopy concept; a de facto text based screenshot.

The Fun Stuff – Screen Window Types

And just when we thought we ‘d seen it all, after the vast array of different options, all the possibilities of screen.. There is still one more special option to mention. Screen has three different window types.

The first window type is the normal window type we’ve been using all along. The regular window with a traditional shell inside.

The second window is initiated by starting screen with as a first parameter a tty (character special device) device name, like for example /dev/ttya. This command connects screen directly to the character device and allows interaction with for example a null modem serial connection trough screen. To accommodate this, screen takes baud and other relevant settings as parameters. The info command now returns some of the modem control lines in the status line and the break command now sends the standard break signal over the device.

The third type is a telnet window. This is started using //telnet as a first parameter, followed by the host name and optionally a port as the third parameter. Screen will then act as a telnet client and interface with a remote telnet server. As with the device type, the telnet window returns telnet specific information when using the “info” command. The break command sends the telnet break signal to the server. This option is only available if screen was compiled with the BUILTIN_TELNET option.

  • Or connect to a serial terminal?

Its in the end as simple as ..

screen /dev/ttyS0 19200 1n8

This might not seem too spectacular as an option, but not needing an extra program and instead being able to use good ‘ol screen can be a good and quick solution for when you need this, instead of installing minicom or whatnot.
And remember .. “A weird imagination is most useful to gain full advantage of all the features.” (Listed as a bug under the screen man page.)

The articles in this series were updated to; These articles focus on the following topics..

Published by Gert


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: