Linux Commands SERVER MANAGEMENT Tips & Tricks

How to use linux screen command

Screen is a window manager or program that allows you to use multiple windows or in other words, multiplexes a physical terminal among several processes, typically interactive shells.  Before we explain about screen command’s usage or in other words screen tips, lets examine the advantages of using a screen session.  

 

In simple words, screen allows you to:

  • Initiate multiple shell windows from a single ssh connection.
  • Keep the session (shell) active even if there are network disruptions.
  • Disconnect and re-connect to an existing session from multiple locations.
  • Execute a program or a long running process without the need to actively monitor/maintain.

 

Installation of screen using yum

There are chances that screen might already been installed in the machine.  You can verify it using the below command.

 
[vimalt@vimalt-linux Desktop]$ which screen
/usr/bin/which: no screen in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/vimalt/bin:/sbin:/usr/sbin)

 

If the screen is not installed, please install it using yum.  Please skip the installation if its already been installed.

 
[root@vimalt-linux ~]# yum install screen
 Loaded plugins: security
 ol6_latest_local 23950/23950
 virtualbox 21/21
 Setting up Install Process
 Resolving Dependencies
 --> Running transaction check
 ---> Package screen.x86_64 0:4.0.3-16.el6 will be installed
 --> Finished Dependency Resolution
 Dependencies Resolved
 :
 :
 Installed:
 screen.x86_64 0:4.0.3-16.el6
 Complete!
 [root@vimalt-linux ~]# which screen
 /usr/bin/screen
 [root@vimalt-linux ~]#

 

Now you can start the screen session as below.

 
[root@vimalt-linux ~]# screen
[root@vimalt-linux ~]#

 

Control commands in screen session

The screen uses “Ctrl+a” combination of keys as control keys to send commands to the screen session.  Please run “Ctrl+a” and then”?”.  This will take you to the screen help page.

 
Screen key bindings, page 1 of 1.
Command key: ^A Literal ^A: a
break ^B b dumptermcap . info i meta a pow_detach D reset Z title A xoff ^S s
 clear C fit F kill K k monitor M prev ^H ^P p ^? screen ^C c vbell ^G xon ^Q q
 colon : flow ^F f lastmsg ^M m next ^@ ^N sp n quit \ select ' version v
 copy ^[ [ focus ^I license , number N readbuf < silence _ width W
 detach ^D d hardcopy h lockscreen ^X x only Q redisplay ^L l split S windows ^W w
 digraph ^V help ? log H other ^A remove X suspend ^Z z wrap ^R r
 displays * history { } login L pow_break B removebuf = time ^T t writebuf >

 

Key bindings are the commands the screen accepts after you enter “Ctrl+a” control command.

 

Various useful tips in screen session

  • Create new window –> “Ctrl+a” “c”This will create a new window and keep your old window alive.  You can create multiple such new windows and perform different actions of your choice in those windows.

 

  • Switch between windows –> “Ctrl+a” “n” / “Ctrl+a” “p”

The screen allows you to switch forward and back among opened windows.   “Ctrl+a” “n” takes you to the next available window and “Ctrl+a” “p” to the previous window.

 

  • Detach from screen session –> “Ctrl+a” “d”

This is one powerful option available with the screen.  You can detach from the screen and re-attach to it at a later point of time.  This makes it easier to detach from one location and re-attach from another place.  This will take you back to shell from you initiated the screen.

 

  • Re-attach to screen session –> “Ctrl+a” “r”

If there is only one screen session alive, it will re-attach to that session.  You can check the list of active screen session by issuing the following command.

 
[root@vimalt-linux ~]# screen -ls
There are screens on:
 5308.test1 (Detached)
 5330.test3 (Detached)
 5319.test2 (Detached)
3 Sockets in /var/run/screen/S-root.
[root@vimalt-linux ~]#

And then connect to the screen of your choice by mentioning the name of the screen as shown below.

 
[root@vimalt-linux ~]# screen -r 5319.test2
[root@vimalt-linux ~]#

  • Log your screen output –> “Ctrl+a” “H”

Sometimes, it is very important to keep track of activities happening in a server.  For occasions like server OS patching, we can perform the complete activities via a screen session after enabling session logging.  Screen will then keep appending data to the file through multiple sessions.  In critical upgrades, if something goes wrong, you can always look back through your logs.  Enter “Ctrl+a” “H” again to stop screen logs being appended.

 

  • Stopping screen session –> “Ctrl+a” “k”

Once you have done with your work, you can kill the screen session using this.  You will get a message if you want to kill your screen and it gets killed once you prompt it with a yes confirmation.

 
Really kill this window [y/n]y
[screen is terminating]
[root@vimalt-linux ~]#

That’s it!

If you like the post and wish to receive more articles from us, please like our FB page: If you like this post and wish to receive more articles from us, please like our FB page: Button

Your suggestions and feedbacks will encourage us and help to improve further, please feel free to write your comments.

For more details on our services, please drop us an E-mail at info@grepitout.com