Friday, 23 May 2008

Power sessions with Screen

Screen is a full-screen window manager for the console, that comes with every flavour of Linux and UNIX. It is best known for multiplexing a physical terminal across several processes. By using it, you can run any number of console-based applications within a single terminal.
For example, suppose I want to run the 'top' program to keep track of the system load, edit my programming code and check my mail at the same time ... Usually, I have to open at least 3 terminals to do all this. But by using the screen utility, I can run all these programs at the same time in the same physical terminal. Another great feature of this wonderful utility is its ability to decouple the terminal emulator from the running program, which ensures your job is running even if you accidentally close your window, or in case you log out. After detaching, you can reconnect once again to the same session and regain control over the running jobs.
Here I will explain how to use this utility.
1) First open a terminal (eg: xterm if you are in X windows) and type :
$ screen
2) Screen starts and creates a new single window with a shell, pretty much like what you had before. The path name of the shell is taken from the $SHELL variable. New windows can be created within the same physical terminal using the screen commands.
3) Now that you have started screen in a terminal, suppose you want to run the 'top' program to check the System load and at the same time you want to compile a program too - Also you would like to switch between the two to see the progress made by both.
For that, first execute the first program to be run (say 'top'). now 'top' will start in the terminal.
Now open a new window in screen by pressing the '[Ctrl + a] c' - which I will state as 'C-a c' . This will create a new window in the same terminal. Here, you can give commands to compile your program.
In screen, each window is given a unique identifier. The first window is numbered 0, the next window is 1 and so on. Now to switch between your 'top' and the compiling program, you can use the key 'C-a 0' and 'C-a 1' respectively.
You can also associate names with each running window - use 'C-a A' to give a name to the current window.
Here are a few other commands that might come in handy while toggling between various running windows :
  • 'C-a p' and 'C-a n' can be used to switch to the next or previous window respectively.
  • 'C-a N' - where N is the number from 0 to 9, that can be used to jump to the corresponding window.
  • 'C-a w' displays a list of all windows. The unique ID of each window with its name and running process is displayed, for each window. The current window is marked with an asterisk(*).
  • 'C-a k' - can be used to kill the current window. You can also type 'exit' to kill the current window. If no more windows are open, then screen exits. 'C-a \' also does the same thing.
  • 'C-a d' - detaches the present screen session. You can also detach by closing the terminal running your screen utility. Though, this appears to close your terminal session, in reality this does not happen. It only unbinds your session from the current terminal. All the programs started under screen will still keep running.
You can also log out from the machine and re-login. Then start any terminal session and type 'screen -r' to once again be connected from where you left.
In case, there were more than one screen sessions running on the machine, Screen prompts for a host.tty.pid.
For example, say I have two screen sessions. So when I type 'screen -r' command, it gives the following message:
$ screen -r
There are several suitable screens on:
2999.pts-6.localhost (Detached)
1920.PTS-6.localhost (Detached)
Type "screen [-d] -r [pid].tty.host" to resume one of them.
Choose the proper tty.host to connect to and you are controlling your program again. You can also share your screen session with others like using VNC . Imagine you are having a problem with your code and want to share it with your boss who is located in another office. You can use screen to share your session with your boss, and he can see what you are referring to. The first user starts his screen in a normal fashion and executes the following commands ...
User1:
[Ctrl-A]:multiuser on [RET]
[Ctrl-A]:acladd
The second user starts a new screen session and connects it to the first session using ...
User2:
screen -x
There are lots and lots of features in Screen other than those that were described here. To know more about Screen, visit the Official Screen Home Page. You can also find a very good introductory tutorial at Kuro5hin.org.

No comments:

Post a Comment