Ever faced a problem with your ssh connection dying in middle of an installation Or in middle of data transfer ? scp rsync stopping in middle due to a ” Connection Closed ” message ?

Linux has a solution for you for this very scenario.  It has a utility that allows you to run any task in the background , without worrying about the connection breaking down due to internet or network cable or any other issue, its called screen. screen will keep things running for you in the background and you can again connect to the screen process and the task would be running as it is.  So how do we install and use it ? . Lets start with the installation.

Installing screen :

You can check if screen binary already exist by using below command :

root@CentOS5Server [~]# which screen
root@CentOS5Server [~]#

If it is already available you are good to go and can move on next step of utilizing this binary/utility.

If its not installed on your machine then you can install it using simple yum command on CentOS , RHEL or any RHEL family distros. You can do it as follows :

yum install screen

Now we can learn how to use the screen command :

Utilizing the screen command :

You can create a simple screen process by just typing the command ‘screen‘ and it will create a screen for you and move the control inside the screen session. Usually its recommended to use some the naming parameters with the command so you can create a screen with a required name and it can be referred to later with that name, specially when there going to be more than one screens and where more than one admins work on one  server. This can done using below command :

screen -mdS TransferingXYZDir

The above command will create a screen process in background with name  ‘TransferingXYZDir’ , you can choose any name. This will not switch the control to the screen session, the control and cursor will remain in the normal shell.

Writing just ‘screen’ will also create a screen session and will also switch the control and cursor to inside the screen session.

You can see the list of available screen processes at any time using the command ‘screen -ls’ as below :

root@CentOS5Server [~]# screen -ls
There is a screen on:
1468.TransferingXYZDir (Detached)

1 Socket in /var/run/screen/S-root.
root@CentOS5Server [~]#

As you can see the screen we just created above is now available in screen -ls , we can create more screen for our specific purpose and then connect to any of the screen and can also move out of any screen, this is called ‘attaching’ and ‘detaching’ from a screen.

You can connect or attach to any screen using below command :

screen -x TransferingXYZDir

If there is only one screen process then typing ‘screen -x‘ will attach you to that screen. When you have more than one screen process you will need to mention its id or name. The screen session created above can also be attached using command ‘screen -x 1468‘.

Detaching from screen requires a below key combination :

‘Ctrl A’ ‘D’

Pressing the three keys in above sequence will detach you from screen.

The screen process can be killed by only pressing ‘Ctrl D‘ or writing ‘exit‘ will also kill/close the active screen process.

The below demo commands will show the screen attaching/detaching and killing commands execution in sequence :

root@CentOS5Server [~]# screen -x TransferingXYZDir

root@CentOS5Server [~]# This is our Screen.
root@CentOS5Server [~]#


root@CentOS5Server [~]# screen -x TransferingXYZDir

root@CentOS5Server [~]# This is our Screen.
root@CentOS5Server [~]# exit

[screen is terminating] ( by pressing Ctrl D )
root@CentOS5Server [~]#


I wrote the line ‘This is our Screen’ in the screen session to show that the console is changing to a screen from normal shell.

The above commands should be enough to get you started and do all basic functions of using screen. For further details you can see the man pages for screen.

Have happing ‘screen’ ing :)

Tags: , , ,

2 Comments on How To : Install and Use screen in Linux

  1. saqib says:

    very nice tutorial, very easy way to master screen, riz khan u rock

  2. karthik says:

    Very good tutorial and awesome utility

Leave a Reply