Friday, May 11, 2012

"screen" : Unix utility to manage many ssh session

Those who most of time work on Unix like system or GNU/Linux and do not use screen, they are really not leveraging simple but powerful facility provided by  screen utility.

Suppose you are one of many Techies who spend most of time works on command line in a ssh session and start many many program on same system or need login on many many system (40) at same time screen is for you.

There is high chances that you login on some of system almost daily and need 10 or 15 or 30 or 40 server login session at same time - all active, whole month !!  screen is your pet.

QuickStart on a Linux system

=> login toleast critical system

=> Check screen package is installed
     $ rpm -q screen
     screen-4.0.3-1.el5_4.1

=> Create a .screenrc file in your home directory similar to this.  Another example is here
$ cat /home/nasimuddin.ansari/.screenrc
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "
###    Keybindings    ##
#
## bind <F7> to detach screen from this terminal
## bind <F8> to kill current session
## bind <F10> to create a new screen
## bind <F9> to rename an existing window
## bind <F11> to move to previous window
## bind <F12> to move to next window
bindkey -k k7 detach
bindkey -k k8 kill
# space in keyboard
bindkey -k k; screen
bindkey -k k9 title
bindkey -k F1 prev
bindkey -k F2 next
displays
multiuser off
hardstatus string "%{.kG}%-Lw%{.KW}%n%f %t%{-}%+Lw %=%{..G} %H %{..W} %d/%m %C%a "
#Start screen windows with blank title
shelltitle ""
# name tiltle window on F1
bindkey -k k1 title
bindkey -k k2 windowlist
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-END =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #

=> AND your are ready to use ! Just type screen on $ prompt !
=> My config file has following key mapping:
F1   -  Set a particulate window title e.g. server101
F2   - Show list of all 40 windows I have open
F8  - Kill a window  (Message : window number 34 killed)
F10 - Create new window to run something on this system or open a ssh session to another system
F11 - Go to just previous windows
F12 - Go to Just next window
Cntr-a Ctrl-a - Toggle between 2 windows
Cntrl-d : Detach screen session. It will keep all your session live. Leave office and go home !

=> I know, on reaching home, you are going to work again and need to start where you left. See all your screen session :



$ screen -list
There are screens on:
        5842.pts-0.MyDevSystem01    (Detached)
        10634.pts-22.MyDevSystem01   (Dead ???)


=> Bring your session to life - re-attach it

    $ screen -r 5842.pts-0.MyDevSystem01

=> Remove a dead session

    $rm -rf /tmp/10634.pts-22.MyDevSystem01


=> If I am not working in a session, I will run "vmstat 500" and leave it breathing until I need it use it  again.

Sharing screen session with another user

=> Start a screen session using


# screen -d -R user1_session

(Replace user1_session with any other helpful name for the session you’re about to create.)

=> Pick up phone and call other user to connect to your screen session. 

# screen -x user1_session

(Assuming another user is logged in using the same user account)

Multiple persons can type and work on the same terminal . Dimensions of terminal output will be the same for every user. A user can change his screen session dimensions - press Ctrl-A and then Shift+F. It will make the screen output fit your current terminal size, and change it to that size for every connected user.

Reference for hungry people:

man screen
screen -h
http://www.ibm.com/developerworks/aix/library/au-gnu_screen/
http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/
http://magazine.redhat.com/2007/09/27/a-guide-to-gnu-screen/
http://technonstop.com/screen-commands-for-terminal-sharing (share session)