Using screen utility in Linux

We had a requirement of building the file system on around 30 hosts. These hosts have around 2TB of space and takes around 2 to 3 days to build the file system. The file system building that I am referring is to our proprietary application file system which is written on top of unix file system . Looks like the program is not well written , so it takes around 2 or 3 to build the file system. Never looked into the code of the program , so don't know much . Anyway my concern is that I cannot leave my terminal open for 2 to 3 days for file system creation to finish. My colleague told me of the screen utility which has facility of de-attaching from the terminal leaving the program running . I tried using screen and phew! it helped. I invoked the file system creation program under screen session and detached from the terminal after invoking the command . I logged off and went home. Next day , after logging in back to my system tried reattaching to the screen session. I connected and could see the file system creation program still running. So, that is a very useful feature and thought of writing an article on it .

What is screen?

screen is a command line full screen window manager when called , it creates a single window with a shell where we can execute our shell commands. screen facilitates creating more windows from the existing shell , list the open windows and kill existing windows. All these windows run the programs independent of each other . Most importantly , it  facilitates de-attaching from terminal without terminating  the program and allows reattaching  back to the screen session .

Using screen:

To invoke screen , just type screen command in the terminal. You will be greeted with a new window with a shell, similar to your earlier window . You will see [screen 0:bash] to the title bar of your terminal window if your running bash from X. The screen commands has to be executed by pressing ctrl-a followed by one other character . Many screen commands are available . Worth going through  the man page. Some of the common screen commands are :

Ctrl-a,c             Create a new window with a shell and switch to that window.
Ctrl-a,C            Will clear the screen
Ctrl-a,K            kill the current window
Ctrl-a,"             Will list out all the open windows in a screen session for selection
ctrl-a,'               Will ask for a window name or number to switch to
ctrl-a,?               key bindings
ctrl-a,n              To go to the next window
ctrl-a,p              To go to the previous window
ctrl-a,X             Lock the screen session

Now let's look into some of the options that can be used while invoking the screen command .

$$ screen -ls :     Will list out all the screen sessions

e.g   $screen -ls
There are screens on:
13683.pts-1.shopfrobsoon-dr (Attached)
13333.pts-1.shopfrobsoon-dr (Attached)
13233.pts-1.shopfrobsoon-dr (Detached)
3 Sockets in /var/run/screen/S-zaman.

$$ screen -S  : To name a screen session

e.g   $ screen -S zaman
       $ screen -ls
       There is a screen on:
       303.zaman (Detached)
       1 Socket in /tmp/uscreens/S-zaman.  (Mac OS)

$$ screen -r  : To resume a detached session.
e.g  $screen -r 13233.pts-1.shopfrobsoon-dr  

$$ screen -d : To detach from a screen session . If there are many screen session attached , it will ask you to provide the screen session which you want to detach.

     e.g $ screen -d
There are several suitable screens on:
13683.pts-1.shopfrobsoon-dr (Attached)
13333.pts-1.shopfrobsoon-dr (Attached)
13233.pts-1.shopfrobsoon-dr (Detached)
Type "screen [-d] -r [pid.]" to resume one of them.

        $ screen -d 13683.pts-1.shopfrobsoon-dr
[13683.pts-1.shopfrobsoon-dr detached.]

       Now let's view the screen session status after detaching using the last command.
$ screen -ls
There are screens on:
13683.pts-1.shopfrobsoon-dr (Detached)
13333.pts-1.shopfrobsoon-dr (Attached)
13233.pts-1.shopfrobsoon-dr (Detached)
3 Sockets in /var/run/screen/S-zaman.

  We can see that the screen session 13683.pts-1.shopfrobsoon-dr is now detached from the screen.

Accessing and Controlling a screen session
Another useful feature of screen is to have control over the sessions and screen provides features to connect to a screen session to monitor all the commands that are being executed in the screen session . This is very helpful to demonstrate any unix practical sessions remotely . Like a friend of you wants to learn linux and he requested you to give a practical session on Linux commands . In that case , you can open up a screen session and request your friend to connect your screen session and demonstrate any basic linux command to complex task like kernel compilation . I feel that this can be greatly beneficial to the corporate world where companies used to spend a lot of money sending there employees to other head office for training. Instead of sending the employees to head office , a screen session can be initiated on the head office and ask the mployees of branch office to connect to the session. screen supports multiple users to connect to a screen session and also supports communication in both ways . If you donot have faith on the person who is connecting to your screen session , then you can give him read only access . In such a case , the connected person will only see what commands you are executing, he  will not be able to execute any command .

Let's now see a practical demonstration of this. We will take a scenario where User A  is initiating a screen session  and another user (User B) will connect to the screen session initiated by User A . The following are the steps:

1.  User A (unix login:zaman) initiates a screen session and name it as  test-session .
              $ screen -S  test-session
              $ screen -ls
        There are screens on:
                18374.test-session      (Attached)
        1 Sockets in /var/run/screen/S-zaman.

2.  After initiating the screen session , we have to enable multiuser mode so that user can connect to
    the screen session. We also need to put acls which user can connect to the screen session. Since
    these are screen commands , precede them using ctrl-a

             ctrl-a, :multiuser on     (enabling users to connect to screen session)
             ctrl-a, :acladd student  (allowing user studen to connect to screen session )

2. Now to allow the remote user to connect to the screen session , we have to set SUID  bit on the screen command . Otherwise the remote user will not be able to connect to the screen session. But remember after after setting the suid bit , looks like we can only connect to the screen session,other screen commands we cannot execute . Need to check further on this.
             $ sudo chmod u+s /usr/bin/screen

Note:Before you set the setuid bit on the screen command , list the screen sessions and inform the user of the screen session_id.

3. Now we are in a position to ask UserB to do ssh to the remote system where screen session is running and after that ask him to connect to the screen session using screen command.

             $ ssh -l student
             $ screen -x  zaman/18374.test-session  (zaman is the unix username who has initiated the
                                                                             screen session )

  UserB (student) will  now be connected to the  screen session and his screen window will change to
  UserA's (zaman) bash screen . Any commands typed by UserA will be visible to UserB along with
  the  output . Also , UserB can also  type any commands which will be visible to UserA. If you are
  paranoid about security , then you may would like to give  read only permission for  users who
  connect to your screen session.


Popular posts from this blog

PSSH : Parallel SSH to execute commands on a number of hosts

How to add check_http as a service in Nagios Monitoring using NRPE

Configuring Nagios to monitor services using NRPE