A dummies introduction to GNU Screen
With graphical user interfaces becoming more and more friendly and easy to use, new users of GNU/Linux and the BSDs can now get their daily work done without having to tinker around (very often) with a terminal. However, many users don't like to use a GUI for every task, and find that they necessarily need to depend on some terminal, or shell. Sometimes, one has to log in to a remote machine, without a GUI. At other times, we just have to run a `console' based application. Under such situations, there are several ways in which a utility like screen can be put to efficient use.
Without describing screen in detail (it's done wonderfully in the manuals and tutorials), let me just give you a typical situation which many of you may be able to relate to and describe how screen helps me be more efficient:
- I really like xterm and use it for all terminal related stuff. However, I really don't like the fact that it doesn't have multiple tabs. Yes, I know konsole, GNOME terminal, mrxvt etc. support tabs, but they really don't have the flexibility of mapping keystrokes to start arbitrary applications in new tabs, which screen can do, and I'll show you how.
- I have my desktop in my room, say A, and another one in my lab, call it B. I need to access a program X on the lab machine, and it takes a really long time to run it. Now, if I start the program in the morning, by sshing from A to B, I should be able to close the ssh session, and still have the application running in the same state, so that if I go to the lab and log into B, I should be able to `reconnect' to the shell running the same instance of program X, which has just been active all the while. Not only that, I can also access the same instance of X by sshing from somewhere else into B. Whew, that was a mouthful! But it made sense, right?
So, I am going to show you how screen comes to our rescue for both these situations. This is going to be more of explaining how I achieved my goals, and less about the details on why I am doing it, or what other (and better) ways of accomplishing things exist in screen. I will just mention some tips at the end, and point you to the manual to learn more, as my intention is to get you to learn screen for the power and flexibility it offers while being very simple.
Starting screen
OK, fire up your favourite terminal emulator (or go to a virtual
terminal) and type screen
. And that's it!
OK, well, if you do that, you would, in all likelihood, notice nothing special, and observe that another shell has come up. But you can be sure that you are running screen in quite a few ways. One simple *but not foolproof) way, is to check the TERM environment variable.
echo $TERM
In all likelihood, it'll be `screen', instead of vt100, linux etc., confirming that you are running screen. Now, whenever I learn a new application, the first thing I like to do is to find out a sane way to exit it, so that I can bail ourselves out of trouble later, if needed. So, just exit the shell normally (by typing exit at the prompt or Ctrl+D for most shells), and you should observe that screen terminates. So, now that screen is ready and working, let's put it to good use.
The prefix key
For all screen commands and shortcuts, you need to enter a prefix
key. By default, this prefix key is bound to Ctrl+A (C-a
). So, for
example, to create a new window, you would type C-a
followed by
C-c
. Now, C-a
is quite a bad shortcut for people like me, who are used
to the behaviour of several applications, such as readline, emacs
etc., which use C-a
to go to the beginning of a line. To work around
this, one can type C-a a
to get the desired effect in screen, though
having to do this every time is a pain, and I describe at the end how
to map a different key as the prefix key for screen.
I am, however, assuming C-a
to be the prefix key for the rest of this
document, though you can just substitute it with the new bound key if
you've read ahead; everything else remains the same.
Creating and using new windows
Once you are in screen, it is very simple to manipulate and switch
windows. All you need to do to create a window is to type C-a
c
, and you're in a new window. Of course, there doesn't
seem to be much difference between the windows, but here's a
simple way to check; in a window, dump some text (like typing ls
/usr/bin
). Then, create a new window using C-a c
. Then
keep switching using C-a n
(for next window), and observe that
you actually are switching between the two different windows.
Now, create some windows, and do some stuff in each of them. Now, we've
really lost track of what we created, right? How do we get a list of open
windows? Simple, just do C-a "
, and browse to the window
you want using the arrow keys and press enter to go to it.
Of course, most of the windows seem indistinguishable, as they seem to
be titled with the name of the shell they run (bash, in my case). So, it
would be better to name them, for your reference. To name a window, all you
need to do is, type C-a A
, and modify the name to whatever you
want. If you get confused, C-a " will provide you with a list of
windows. This should get you up and away with creating multiple windows in
the same terminal. My example screenrc
below shows a way to mimic
a multiple tab display which displays window names and numbers, which may
also be useful.
If you want to `close' a window, the best way would be to exit the shell
(or application) running in the window. In case you are unable to close the
application because it's hung, you could also kill the window and all its
applications using C-a k
, but do avoid this when possible.
Exiting all active windows closes screen, and a message is displayed
indicating that.
This should give you a feel for some of the capabilities of screen. Often people have several applications running in each window of their screen, and they label the window for easy access. For example, I often run Mutt (a mail client) and irssi (an IRC client) on screen, and leave them running for easy access. Now that we are done with the window multiplexing part, let's go to the detaching and re-attaching part, and see how you can access your running applications even after closing the terminal, or from a remote location (by sshing to your machine).
Switching windows
There are several ways to browse across various windows in screen. Here are a few:
- As described above,
C-a "
displays a menu from which you can choose the window. -
C-a n
andC-a p
can be used to go to the next and previous windows respectively; nice to cycle across consecutive windows. -
C-a
<number> is another easy way to jump to another window.
Once you are comfortable with moving around in screen, we can move to the next feature, which I address as "workspace persistence".
Workspace persistence: detach and reattach
So, you're in the middle of hacking this wonderful software which is going to change the way computers are used world over. (Or, you're just playing one of those console based games and think you can finish it this time). Now, you realise that it's time for work, and you have to leave! Don't panic, there is something you can do to retain all applications running in your screen windows as they were, so that you can either come back later and resume your work (or game), or connect to your computer from a remote place (via ssh, say) and resume the activity. Sounds fun, huh?
The way of achieving this on screen is by detaching and reattaching
screen. To leave all applications running in all your windows as they are,
you can do one of two things, depending on the situation. The easiest way,
if you are running screen from an X based terminal emulator (like XTerm,
gnome-terminal, konsole etc.), is to just close the terminal emulator
(using the exit button or File-Quit or one of the other standard
techniques). That way, screen catches the signal sent to it by the terminal
emulator, and keeps itself running even while allowing the terminal
emulator to close. The other (and more obvious) way to detach from a screen
is using the C-a d
key combination, which returns to the shell
from which you started screen, with a "[detached]" message.
This is probably the only elegant way to detach if you are on a tty
terminal. To reattach to the running screen, all you need to do is to start
the terminal (or get to a tty terminal) and type screen -r
, and
you will return to the same state in which you detached. In case you have
several screens deattached, you will get a list of available screens to
which you can attach. In such a case, just append the screen
-r
command with the process ID of the screen you wish to attach to.
Also, in case you forget to detach from your screen before trying to
reattach from another location, you would need to detach and then reattach
using screen -dr
.
To convince yourself that the applications in the screen windows
really keep running even after detach, you could perform this simple
test: Fire up screen and start some program, say a text based browser
like elinks
or top
, or start a really big download
using wget
. Now, detach using the method described above, and
do ps ax|grep <appname>
to verify that the application
is still running. For example, I ran elinks
from within
screen, detached, and sure enough, ps ax|grep [e]links
gave
me output containing elinks.
This way, you have the flexibility of opening up several applications in any number of windows, and the ability to conveniently access them from anywhere, as long as you can log into the computer on which you run screen.
Other commands and features
There are other features of screen which I am not mentioning here, since
I really haven't explored them myself yet. However, note that you can read
the man page to get a fair idea of screen's capabilities. For help while
using the application, you can get a quick list of commands and the keys
they are bound to using the C-a ?
key combination.
A screenrc (customisation) example
Before ending this introduction to screen, let me just give you a brief introduction to customising screen using the $HOME/.screenrc file, so that you can retain the settings for the next time you start up. Do read the screen man page for more information on the syntax and purpose of these commands.
# First of all, I really don't like C-a to be my key combination, since # I use it a lot to get the same effect as the `Home' key. So, I choose # C-\ as my preferred shortcut key by adding this line to my ~/.screenrc. escape \034\034 # (If you are wondering, \034 is the key sequence C-\ in octal. Please # read the documentation for `escape' in the screen man page to find out # how I arrived at that). # Prefer an audible bell to the visual one. vbell off # Skip the startup message startup_message off # Display a caption string below, appearing like tabs and # displaying the window number and application name (by default). caption always caption string "%{kw}%-w%{wr}%n %t%{-}%+w" # Special keys. C-a m will spawn Mutt, for example, on window 7 (if it # is available), or the next available window. bind m screen -t 'Mutt' 7 mutt bind e screen -t 'Elinks' 8 elinks www.google.co.in bind i screen -t 'Irssi' 9 irssi
This is a really simple screenrc, made by shamelessly copying from example screenrcs available from others' websites and blogs. You can get much better screenrcs by just searching the net, and finding out the innovative possibilities of customising screen.
Conclusion
This was my attempt to provide you a glimpse of the often used features of screen which I believe would be helpful to all terminal junkies who like to do a lot of switching between applications. Once you get started, there are many documents and tips you will find online to continue learning the advanced features of screen, though I would recommend a thorough reading of the screen man page to know the full capabilities of this wonderful software. I particularly recommend the information about monitoring window activity and splitting screen windows.
Comments and feedback welcome. Thanks!
Talkback: Discuss this article with The Answer Gang
Kumar Appaiah is studying to earn a B.Tech in Electrical Engineering and M.Tech in Communication Engineering (Dual-degree) at the Indian Institute of Technology Madras. He has been using GNU/Linux for the past five years, and believes that Free Software has helped him do work more neatly, quickly and efficiently.
He is a fan of Debian GNU/Linux. He loves using Mutt, GNU Emacs, XCircuit, GNU Octave, SciPy, Matplotlib and ConTeXt.