Power Sessions with Screen
Screen is a terminal multiplexer that allows you to manage many processes through one physical terminal. Each process gets its own virtual window, and you can bounce between virtual windows interacting with each process. The processes managed by screen continue to run when their window is not active.
Thus far, the screen features described aren't all that exciting or new. In fact, there already are X11 terminal applications that provide this functionality (konsole and multi-gnome-terminal). What differentiates screen from the others are some of the core features screen provides.
Screen offers the ability to detach from a session and then attach to it at a later time. When detached from a session, the processes screen is managing continue to run. You can then re-attach to the session at a later time, and your terminals are still there, the way you left them.
Screen also maintains individual, searchable scrollback buffers for each of the windows it manages. You can perform traditional “enter the search term and I'll find it for you” searches as well as incremental searches. This is such an obvious feature, it's surprising that more terminal emulators do not offer it.
Other notable features of screen are configurable key bindings, utf8 and multibyte charset support, multi-attach, configurable input and output translation, input and output filter, multi-user support with access control lists (ACLs) and logging.
Before actually running screen, it's important to understand how to interact with it. Screen sends all entered text to the current window, with the exception of the command character. The default command character is Ctrl-A (press the Ctrl and the A key at the same time). The screen man page uses C-, Emacs style, to mean Ctrl-.
The command character is used to notify screen that you'd like to control screen itself, rather than the application in the current window. The key pressed after the command character designates which screen command you would like to perform.
Some of the more useful commands and their key bindings are shown in Table 1.
Table 1. Commands and Their Key Bindings
For many of the commonly used commands, the control version of the key is also bound to the command. An example of this is Ctrl-A C and Ctrl-A Ctrl-C to create a window.
To send Ctrl-A to an application without screen intercepting it, you can press Ctrl-A A. The command character can be changed to an alternate key if you wish. Typically Emacs users change the command character to Ctrl-B by adding escape Bb to their .screenrc. The following examples use Ctrl-A because that is the default.
As you might expect, .screenrc is the per-user configuration file in your home directory, and /etc/screenrc is the system-wide configuration file that applies to all users.
Now that you understand the fundamentals of interacting with screen, we can step through screen fundamentals using a typical screen session as an example. Probably the most typical use of screen is to control terminals on a remote machine on which you have a shell login.
So, for those of you playing along at home, log in to a remote host that has screen installed. If you don't have a remote host to ssh to, you can install screen and ssh to localhost. Packages for screen are available for most distributions.
You're now sitting at the shell prompt on the remote machine. Type screen at the prompt. You should see a splash screen showing some information explaining that screen is under the GPL, where to report bugs and so on. You can press the spacebar to bypass this screen (you can disable this splash screen permanently with startup_message off in your .screenrc). Next, you should arrive at another shell prompt, this one running inside of screen.
The new shell running inside of screen should behave like your first shell would. If you do a printenv, you may notice a few new environment variables set. Screen sets TERM to screen—each screen window provides its own vt100-compatible virtual terminal. The variable WINDOW is set to the virtual window number, and the variable STY is set to your session name. I'll explain more about those last two later.
So far, this single shell works exactly like you're used to working on a remote machine. For the sake of this example, say we are downloading the current version of screen (as of this writing, screen 3.9.13) from the screen distribution site at ftp.uni-erlangen.de/pub/utilities/screen. While this file is downloading, you decide to use your spare time to clean up your home directory. If you weren't using screen, you'd have to open another xterm and ssh to the remote machine. With screen, a simple Ctrl-A C will create a new screen window with a new shell process.
Thus far, you have an FTP client and a shell busily tidying up your home directory. You can check on your download in the original FTP window by using Ctrl-A P to go to the previous window. You can get back to your shell with Ctrl-A N.
When you check on your download, it's not finished (screen doesn't take that long to download, but we'll pretend it does for this demonstration). Time to get back to your messy home directory, right? Before doing that, press Ctrl-A Shift-M to monitor the current window for output. Now screen will notify you when there is activity in the FTP window. Bouncing between windows to check on your download's progress is no longer necessary. This also works in the inverse case; use Ctrl-A _ to monitor for silence (30 seconds by default). Monitoring for silence is useful for long compile jobs or other things that spew information.
You can continue to spawn new shells and do things in parallel on the remote machine. After you open a few windows, it becomes difficult to keep track of which window is where. This is where the windowlist comes in to play. Press Ctrl-A “, and you will be presented with a list of the current open windows. Navigate the list with the J and K keys. Pressing Enter on an entry will make it the current window. By default, the window name isn't all that descriptive. You can remedy this by setting the window name yourself with Ctrl-A Shift-A. You can set these titles automatically, much as you would set titles in an xterm, by sending Esc-K, then the title, then Esc-\. Most likely you can adapt a shell-specific recipe for setting the xterm titlebar to the screen window name using the mentioned escape sequences.
To finish up our basic run-through of windowing, let's close your existing windows. If you exit the shell that screen has spawned, the window is deleted automatically. You can delete a window manually with the kill command (default Ctrl-A K). When you exit all of the screen windows, screen exits. You also can tell screen to exit and kill all of your windows by issuing the quit command (Ctrl-A \).
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Build a Skype Server for Your Home Phone System
- Why Python?
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Validate an E-Mail Address with PHP, the Right Way
- Tech Tip: Really Simple HTTP Server with Python
- Understanding the Linux Kernel
1 hour 13 min ago - General
3 hours 42 min ago - Kernel Problem
13 hours 45 min ago - BASH script to log IPs on public web server
18 hours 12 min ago - DynDNS
21 hours 48 min ago - Reply to comment | Linux Journal
22 hours 20 min ago - All the articles you talked
1 day 44 min ago - All the articles you talked
1 day 47 min ago - All the articles you talked
1 day 48 min ago - myip
1 day 5 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Re: Changing escape key sequence for screen.
Far better is to use vi which doesn't require the use of control/meta keys!
Re: Changing escape key sequence for screen.
C-g is also critical for canceling commands...
Re: Changing escape key sequence for screen.
Alternatives include:
escape ^Oo (C-o opens a new line in emacs)
escape ^Pp (C-p moves the cursor to the previous line)
Re: Changing escape key sequence for screen.
Five years ago my screen escape and literal command characters of choice for Emacs noninterference:
escape "^^^^"
Using Ctrl-^ was a bit awkward but I got used to it. If I were using screen as much nowadays I'd probably choose a different character, maybe even a function key.
The first thing I looked for
The first thing I looked for when I started using screen was an option to change the escape key - I didn't fancy having to hit C-a a to move to the start of the line.
After thinking about it for a while, I came up with this:
escape `\'Which I'm a big fan of. For some reason hitting ` ' to get a backquote just feels intuitive (though it might help that I use dvorak so ' is where q is on qwerty).
Sessions for X
I love screen. But what I want is something that can do stateful sessions with X programs when my X server crashes. VNC is to slow, and Xmove I couldn't get to work. Any suggestions?
My address is davidmccabe ta myrealbox tod com
if you'd like to contact me about that.
Thanks!
Re: Sessions for X
have you looked at ratpoison? (http://ratpoison.sourceforge.net)
I thought I remember reading somewhere that you could detach and attach
sessions...but I can't seem to find it documented anywhere now. Anyway,
if you like screen, you might like ratpoison.
Re: Sessions for X
Yes, I use Ratpoison right now, and no, it doesn't do that.
Re: Sessions for X
http://palm.freshmeat.net/articles/view/581/