2008-11-09

The Wonderful Joys Of SCREEN

    Ever logged on via SSH to a machine and started a long running process and then realize that you need to shutdown your laptop and take it with you? Ever accidentally get disconnected from an SSH session and lose hours of work? Ever started a command that's going to take longer than you expected and you want to do some other things while you're waiting? If you have, then you need to learn the joy that is screen.


    Screen is a wonderful little utility for *NIX command line interfaces. It allows you to have multiple shells open with only a single telnet/ssh session, and best of all it allows for disconnecting from a session without terminating the applications therein. This means that never again will you be stuck waiting on a command to complete before you can move on to your next task.

    I use screen daily at work for maintaining the state of my shell sessions, and screen is available on every Linux distribution I have ever installed. It makes short work of switching between shells and creating new shells within a single session. To make it even easier to use, I put the settings to start screen into the RC file of my favorite shell (bash). Here's how I modify the /etc/bash.bashrc on Ubuntu Intrepid Ibex to handle screen automatically when I log on via SSH:


  1. Open the /etc/bash.bashrc file in a text editor

  2. Indent all of the existing lines

  3. Insert the following items at the top of the file

    • if ! [ "${TERM}" = "screen" ]; then

    •     screen -d -R

    •     exit

    • else


  4. Insert the following line at the bottom of the file:

    • fi


  5. Save the file


    What this little bit of code accomplishes is that it automatically starts screen when you log in, but it only starts it once. Once your shell is started inside of screen it just loads the normal bash profile settings. When you end your screen session by exiting or disconnecting, it also exits you out of the parent shell.

    Now, whenever you log on to that machine and open a bash shell, screen will start automatically and either reconnect to your last screen session (if you disconnected from it), or it will start a new screen session.

    Now that we have screen set to start every time we log in, we'll need to know how to use screen properly. Instead of re-inventing the wheel on this one, I'm going to point you to a wonderful quick reference that I use HERE. Or better yet, since you are already becoming better in the *NIX shell; just type man screen.

Like screen? Hate it? Leave some comments!!!

2 comments:

Anonymous said...

Screen is indeed "The Berries".

hardstatus string '%c %06= %-w%{wk}%n %t%{-}%+w %-= %l'

Deven Phillips said...

I kinda like this one:

caption always "%{+b rk}%H%{gk} |%c %{yk}%d.%m.%Y | %72=Load: %l %{wk}"

Shows the hostname, current time/date, and system load at the bottom of your terminal. Very nice.