Screen - Take your terminal session with you!

Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). In other words You can run multiple applications in a terminal session that allows you to 'detach'(disconnect) the session by running the applications in the background and 'attach'(reconnect to) the same session later.

Imagine these scenarios:

Case 1:
1. You scheduled a long job from the terminal in your Office desktop.
2. You are in your colleagues office/ home/ attending a conference on east coast. Want to check the status of this job?

Case 2:
1. You have submitted the job from your laptop.
2. But now you cannot shutdown your laptop because the job will get killed.

Case 3:
1. You submitted the job from a machine .
2. The network for that machine goes down for 2 minutes. But now, your job is also killed!!

Screen is a program that comes to your rescue in these scenarios and more

With screen you can...

1. Close and open the terminal's session from any computer
2. Share the session with colleagues
3. Stay immune to network downtime and laptop battery draining out

Using Screen

To start a new screen, simply execute the following command

$ screen

You'll see a message telling you the version of screen you're using and a bit of license information. Just hit space or Enter to get the shell prompt again. Now you are running inside screen. We can now test that its possible to disconnect and reconnect this screen session by running a simple command:

$ ls

and you should get a normal directory listing. Now press ^a-d (Ctrl-a and then 'd') and you'll exit the screen session and see a small [detached] message displayed to tell you this. We can see that screen is still running by using the list command

tpradeep@helios:~$ screen -list
There is a screen on:
        4533.pts-0.helios       (08/10/2009 04:36:10 AM)        (Detached)
1 Socket in /var/run/screen/S-tpradeep.

tpradeep@helios:~$ 

This shows me that there is one session currently running, and that 4533 is its process ID. we can now retrieve it by typing:

$ screen -r

If there were several screen sessions running then you would have to specify which one you want to retrieve, and you do this by specifying  the process ID. For example 'screen -r 4533'.

To end a session, simply exit the last running application (this may be a shell) and you'll get a message like [screen is terminating] to let you know what's going on.

You can also specify a meaningful name while creating a new session.This name will identify the  session for  "screen  -list"  and  "screen -r" actions. It substitutes the default [tty.host] suffix.

$ screen -S matlab_dartel

you will start a screen session called 'matlab_dartel'.
start matlab by typing 'matlab' at the command prompt(it will open in nodisplay mode by default), do a small task.

tpradeep@helios:~$ matlab
Warning: No display specified. You will not be able to display graphics on the screen.

 < M A T L A B (R) >
Copyright 1984-2009 The MathWorks, Inc.
 Version 7.8.0.347 (R2009a) 32-bit (glnx86)
 February 12, 2009

To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

>> eye(2,2)

ans =

     1     0
     0     1

>>

Detach the session by pressing ^a-d. Check the status  of  your  sessions by means of "screen -list".

tpradeep@helios:~$ screen -list
There are screens on:
        5260.matlab_dartel      (08/10/2009 04:52:16 AM)        (Detached)
        4533.pts-0.helios       (08/10/2009 04:36:10 AM)        (Detached)
2 Sockets in /var/run/screen/S-tpradeep.

tpradeep@helios:~$

Reattach the matlab session by typing 'screen -r matlab_dartel'. You will get back to the same matlab session you started before.

Note:The session has to be detached before you try to attach or alternatively you can do both the things at the same time by typing 'screen -D -R matlab_dartel'

-D -R  options mean: If a session is running, then reattach. If necessary detach  and  logout  remotely first.   If  it  was not running create it and notify the user.

Multiple tabs

You can have multiple tabs inside your screen session

Like Firefox is one process but multipe websites can be opened in each tab

Inside a screen session, you can have multiple tabs. ^a-c creates a new console inside the session, you can switch between tabs using ^a-"nr of the tab" E.g ^a0 takes u the first tab, ~a1 takes u to next tab etc...

Key Bindings

Here is a default list of commands that can be used inside screen

Ctrl-a cCreate new window
Ctrl-a kKill the current window
Ctrl-a wList all windows (the current window is marked with "*")
Ctrl-a 0-9Go to a window numbered 0-9
Ctrl-a nGo to the next window
Ctrl-a Ctrl-aToggle between the current and previous window
Ctrl-a [Start copy mode
Ctrl-a ]Paste copied text
Ctrl-a ?Help (display a list of commands)
Ctrl-a Ctrl-\Quit screen
Ctrl-a D (Shift-d)Power detach and logout
Ctrl-a dDetach but keep shell window open

Sharing Screen

You can connect to a screen session multiple times. This allows you to use the same screen session on multiple terminals (potentially in differing locations). Connect to one screen session as standard, and then use screen -x to connect a second terminal to the same session. You can then see the same output on both terminals.
Note: You need to use the same username for both screen sessions for this to work.

References:
http://www.ibm.com/developerworks/aix/library/au-gnu_screen/index.html
http://www.hantslug.org.uk/cgi-bin/wiki.pl?LinuxHints/Screen