understandable linux tips, tricks and tutorials

Screen is a terminal application that is not that well known outside of hardcore linux geeks, but once you get to know it and understand what it can be used for, it can be pretty useful. So what is screen? Well, it’s like virtual desktops for the terminal. Back in the bad old days when window managers were pretty basic and getting X Windows up and running was a major triumph, having the ability to have unlimited terminal shells was a key feature of screen. These days with modern desktops, this isn’t so important, however, if you access linux machines remotely, having screen available on a remote machine can be very useful.

Starting Screen

Screen can be started by the simple command:

::Tue Mar 30, 07:51 PM::/home/matt2::16 files::359Mb::
matt2@linux-f215 ==> screen

This should result in a terminal window that looks a bit like the one shown on the right. As you can see, the terminal looks just like a new terminal window. However, down the bottom there is a line with a few status messages. I’ll talk about the kind of information you can include in that status bar in the section below relating to the .screenrc file.

To get back to your original terminal, you simply press ctrl+a then ctrl+d. ctrl+a tells screen that you’re about to use a screen shortcut command and ctrl+d tells screen to detach the current screen. Although it looks like the screen terminal has closed, it hasn’t. It has just been detached, and continues running in the background. This means that any applications started in that screen keep running. For example if you were compiling a large program, you could open a screen, start the compile, detach the screen, and then come back to it later. This can also be particularly useful where you are running a program that takes a long time on a remote machine via ssh and need to break the connection, or you lose the connection. If the program is running in a screen session, the program will continue to run despite the lost connection.

Reattaching to an Existing Screen Session

Once you’ve detached from your screen session, how to you get back into it? If you only have one screen session, type screen -r and you’ll be back in your screen session where you left off. If you’ve got more than one session, you’ll have to specify which screen you want to reattach to. If you type screen -r you end up with this:

::Tue Mar 30, 08:20 PM::/home/matt2::16 files::360Mb::
matt2@linux-f215 ==> screen -r
There are several suitable screens on:
        10641.pts-0.linux-f215  (Detached)
        12266.pts-0.linux-f215  (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

To get back to the screen you want to use, you’ll have to type (for example) screen -r 10641.pts-0.linux-f215. As you can see, this could be a bit of a pain. So one way to deal with that, is to assign a name that you can remember to the screen session. To do this, type:

::Tue Mar 30, 08:31 PM::/home/matt2::16 files::360Mb::
matt2@linux-f215 ==> screen -S test

You can then reattach to that screen by typing screen -d test. Much easier.

Screen Shortcuts

Screen has a number of keyboard shortcuts that can be useful in manipulating and swapping between screens. Remember, before you can enter a shortcut, you need to press ctrl+a. These are the shortcuts:

  • 1 through 9: Switches between windows;
  • ctrl+n: Switch to the next available window;
  • backspace: Switch to the previous window;
  • ctrl+a: Switch back to the last window you were on;
  • A: Change the window title;
  • K: Kills the current window;
  • c: Creates a new window;
  • [: Then use the up and down arrows to scroll the window;

.screenrc

The .screenrc file lives in your home directory, and specifies default settings for your screen session. My .screenrc looks like this:

shell -${SHELL}  # dash (-) is for login shells
shelltitle ''
vbell on
autodetach on
startup_message off 
defscrollback 8192
 
hardstatus alwayslastline
 
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'

For more information on customisations that can go in .screenrc, check out the screen manpage.

More Info

This article barely scratches the surface on using screen. For more information check out the screen manpage.

Leave a Reply

Site last updated October 20, 2012 @ 12:53 pm; This content last updated September 12, 2012 @ 3:21 am