Make Your Life Easier With GNU Screen

July 29th, 2011 | Posted by Quixey in Technology

What do you do if you’re away from your work computer but need to fix a coding issue? Your instinct is probably to SSH in to fix the issue in a text editor. However, command line navigation and text editing often aren’t enough for important tasks, especially when you need webservers, database consoles or multiple shells running at the same time. One option is to open multiple SSH sessions on the same machine, but setting them up every time you connect becomes tiresome.

There’s a much better way to connect to your computer: a free utility called GNU Screen. It creates one or more virtual terminals in which you can run shells and whatever other programs you want.

Virtual terminal screen sessions can last much longer than a single SSH session. Even if your connection is lost, the session will just be detached – invisible, but continuing to run the same programs. Your session lasts until you explicitly kill the last virtual terminal in the session. That means you can open the web servers, consoles and shells you want once, and simply reattach to the same screen session if you need it again.

Virtual screen sessions have rather bewildering commands and a Spartan interface, but a .screenrc configuration file in your home directory can make your screen session more usable. The file displays the names and numbers of the current virtual terminals at the bottom of the screen.

Here is a simple but functional .screenrc file we’ve been using at Quixey:

autodetach on
startup_message off
defscrollback 30000
vbell off
term xterm-color
caption always "%{kB}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{kB}%?%+Lw%?"

Here’s what all of that means:

  • autodetach on
        • Ensures that a sudden disconnection from the terminal (due to internet connection issues, for example) detaches your screen session instead of ending it.
  • startup_message off
        • Turns off the copyright message.
  • defscrollback 30000
        • Gives you 30000 lines of history for every virtual console. You can figure out how to scroll and search through it from the manual.
  • vbell off
        • Turns off a “visual bell” that flashes the screen every time the computer wants to alert you of something.
  • term xterm-color
        • Enables terminal colors.

That brings us to the last line:

caption always %{kB}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{kB}%?%+Lw%?

This beauty formats the list of virtual terminals that’s displayed at the bottom of the screen. The components are as follows:

  • %{kB}
        • Set color to black on light blue.
  • ?%-Lw%?
        • Show the entries for the windows numbered greater than the active window.
  • %{bw}
        • Set color to blue on light gray.
  • %n*%f %t
        • Display the window’s number, flags# and program, for example “3*$ bash”.
  • %?(%u)%?
        • Show what other users are editing this window.
  • %{kB}
        • Set color to black on light blue.
  • %?%+Lw%?
        • Show the entries for the windows numbered higher than the active window.

A session is started by typing screen. If you disconnect, you can can reconnect by typing screen -x. Inside the screen session, ctrl+a is the (customizable) prefix key for all screen commands. The most important of these commands are:

  • ctrl-a c
        • Create a new virtual terminal.
  • ctrl+a A
        • Rename the current virtual terminal.
  • ctrl+a <number>
        • Switch to the virtual terminal of the number provided.
  • ctrl+a ctrl+a
        • Switch to the most recently viewed virtual terminal.
  • ctrl+a d
        • Detach the screen session.

That’s about all you need to know to start using screen like a pro! If you need more guidance and Google hasn’t helped, I recommend trying your luck with the 130+ page manual.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

2 Responses



Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>