Ubuntu Polishes GNU Screen

Friday May 22nd 2009 by Charlie Schluting

The latest Ubuntu release adds a few bells and whistles to make using terminal multiplexer screen more of a pleasure for busy command line junkies.

GNU Screen is a terminal multiplexer program that Linux folk have used for ages. It allows you to turn one terminal into many, and run processes even after logging out. In this article we will give a brief overview of screen usage for the uninitiated, then talk about how Ubuntu’s defaults and new screen-profiles package have taught us about new and wonderful features of screen.

A Short Tutorial

To run screen, simply execute the command screen

You will see a flash, then be at a blank terminal. While it may seem that nothing has happened, what you’re looking at is actually a new terminal within screen. You can continue as normal, but sooner or later you will wish to run a new terminal. First, let’s get familiar with screen.

Pressing the control and a keys simultaneously (which we'll denote with C-a from now on) signals screen to interpret the next command. So if you press C-a and let go, then press ‘c’ for "create," (C-a c) you will be in a new terminal window. You’ve created a new window within screen. You can view the list of windows with C-a w. If you exit your shell in the new window, screen will automatically destroy the window and you’ll be back to the first. Create a few more and type some random commands in each (different ones in each). You can now press C-a then SPACE to move to the next window. If you press C-a a you will return to the previous window. Also, you can select windows directly by typing the number after C-a.

Lastly, you need to know how to detach and reattach to screen. Press C-a d and screen will exit. You are back at the single terminal you started with, without screen. You can see what screen sessions you have running in the background with ‘screen -ls.’ Now, to reattach to your screen session, run: screen -x

Useful Screen Commands and Keystrokes

  • screen -ls : List your running screen sessions
  • screen -x: reattach to your screen session
  • C-a c : Create a new screen window
  • C-a w : View a list of windows
  • C-a SPACE: Move to the next window
  • C-a d : Detach from your screen session

We use the -x command to reattach so that it can be done multiple times. From your laptop, home computer, and work computer, are the most common. For the rest of the basics, see the screen man page or C-a ?.

Things to Do With Screen

Screen is wonderful for work management. You can have a standard layout of screen windows, each one dedicated to a different server. You will instinctively press C-a 8, for example, when you think about the ssh session that generally lives in that window.

Most often, we will want to do something else while waiting for a long-running command to finish. Easy enough: Just create a new window and carry on. This is also very handy for “leaving.” If you ssh to a server from your laptop and start a process or script that takes an extremely long time to complete, you may be stuck. Close your laptop and go home, and the process ends. Of course, you should have run it from your screen session that runs on a server.

Many people also use screen to stay attached to IRC while they are away, and read the chat history of what was missed. Simply run irssi or your favorite IRC client within a screen window, and you’re all set.

Make Your Screen Cooler

Ubuntu’s screen ships with wrapper scripts that prompt you to select a theme the first time screen is run. You may never notice these sorts of changes if you always drag your old home directory along everywhere you go.

If you select ubuntu-dark as the theme, you end up with a few small, but very significant changes to your screen environment. Your ~/screen-profiles/profile will be symlinked to /usr/share/screen-profiles/profiles/ubuntu-dark. This file holds the key to all the magic.

To explain what we’re gushing about, run screen and select ubuntu-dark. You will notice two permanent bars on the bottom. The first lists your current windows, and the second lists vital information about the host you’re running screen on. The bottom line includes uptime, load average, cpu frequency, total and available RAM, and the time (which constantly updates). Of course you can add other things, this is just the Ubuntu default.

To replicate the first bar across the bottom on your non-Ubuntu system, enter the following in your ~/.screenrcfile:

caption always "%{wK}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wK}%?%+Lw%? %= %{= Kw}%110`%109`%111`"

A caption in screen is just what it sounds like. The caption will be the bottom line (until we do something else to make it the second to last) in your terminal window. The escape sequences above are what Ubuntu uses, and you’re free to adjust as necessary.

Finally, the very last line is called the hardstatus. This is actually a function of your terminal, which will keep the last line separate from your normal input/output space. If the terminal type you’re using does not support hardstatus, screen will fake it by using the last line in the terminal to display this information.

To set it as described above, add this to your .screenrc:

hardstatus string '%99`%{= Kw} %100`%112`%= %102`%101`%114`%115`%108`%113`%119`%117`%118`%116`%106`%104`%103`%105`%107`%Y-%m-%d %0c:%s'

Unfortunately, this will only work in Debian-based systems that have the screen-profiles package installed. To get this functionality on other systems, you would need to get screen-profiles installed and working. The commands above such as ‘110 are defined by this package, and while you could replicate this functionality, it would probably be easiest to attempt porting the Debian package to other systems.

We find these settings useful for two reasons. First, the hardstatus bar is very handy for getting a quick status overview of your system. You can easily tell is the load is high or if you’re in a dire memory situation. Second, it unmistakably lets you know that you’re in a screen session, without requiring that you attempt running screen commands to check.

Charlie Schluting is the author of Network Ninja, a must-read for every network engineer.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved