Saturday, October 8, 2011

// // Leave a Comment

Using Screen for Linux Session Management

What is Screen ?
GNU Screen can be thought of as a text version of graphical window managers, or as a way of putting virtual terminals into any login session. It is a wrapper that allows multiple text programs to run at the same time, and provides features that allow the user to use the programs within a single interface productively.

Persistence

    Similar to VNC, GNU Screen allows the user to start applications from one computer, and then reconnect from a different computer and continue using the same application without having to restart it. This makes migration between locations like work and home simple. Screen provides terminal-agnostic functionality so that users can disconnect and reconnect using different terminal types, allowing applications to continue running without being aware of the change in terminals.

Multiple windows
    Multiple terminal sessions can be created, each of which usually runs a single application. The windows are numbered, and the user can use the keyboard to switch between them. Some GUI terminal emulators provide tabs or otherwise similar functionality to this. Each window has its own scroll-back buffer, so that output is captured even when the window isn't actively displayed, and that history can be saved even when migrating to another computer. Windows can be split-screened. While some text applications have this functionality built in, Screen allows any application to be split-screened alongside any number of other applications.

Session Sharing

    Screen allows multiple computers to connect to the same session at once, allowing collaboration between multiple users. The same computer can also be used to make multiple simultaneous connections, providing alternative functionality to screen-splitting, particularly for computers with multiple monitors.
Why we need Screen ?
Here is an example You are logged into your remote server via SSH and happily plucking along at your keyboard and then it happens. Suddenly, the characters stop moving and then you get the dreaded “Connection Closed” message. You have just lost your session. You were halfway through some task and now you have to start over. Ugh. Well you can prevent this from happening by using screen. The Linux screen tool can not only save you from disconnection disasters, but it also can increase your productivity by using multiple windows within one SSH session.
Installing Screen
Most linux are already have screen installed on the system.
if you can find any screen installed on the system. you can install it from yum, apt-get or other repositories
Example installing screen in centos/RHEL
#yum install screen
Example installing screen in BSD / FREEBSD
#cd /usr/ports/sysutils/screen
#make install clean
Screen usually installed in /usr/bin/screen or /usr/local/bin/screen
Using Screen
Simply type screen in your shell prompt like below
#screen
You will see a screen welcome message like below
you can simply press space to continue

How To Using Screen
We need to press CTRL and then press "A" to send a command to screen.
You can reconfigure these keys to your liking using a .screenrc file, but I just use the defaults
Multiple Windows or Multiple Screen Session
#screen
(now we are in screen 0:bash)
Example in screen 0 we will download kernel
#cd /root

(We can open other session with command below)
Press CTRL and A and c
(Now we are in a screen 1 session and then you can see a new SSH prompt again like below)
#
We can exit the screen 1 session, simply type command below :
#exit
And you will back to a screen 0 session above where the wget kernel still running.
We also can detached a session with command below :
(still in a screen 0 session )
Press CTRL and press A and press d
[detached]
#
You will see a detached, which is mean screen 0 session still running in background and we already quit from screen session. but we don't need to worry about ssh lost connection because we can attach to screen again.
How can we attached to a screen 0 ?
First we need to see a screen session list with command below
#screen -ls
There is a screen on:
        26303.pts-3.sitenow     (Detached)
1 Socket in /var/run/screen/S-root.
Now we  can see a screen 26303.pts-3.sitenow (Detached)
we will attach this 26303.pts-3.sitenow  screen session
#screen -r 26303.pts-3.sitenow
(now we are in screen 0 session again )
How about if i have few screen session and i want to back to previous screen without exit the current screen ?
in screen we can press CTRL and A and p for back to previous session screen or
in screen we can press CTRL and A and n for next session screen
For complete reference.
Please take a look screen User's Manual http://www.gnu.org/software/screen/manual/screen.html

0 comments:

Post a Comment