Wednesday, November 26, 2014

Path to linux screen

Screen makes dev/devops life easy in many ways,

1. If you want to keep a shell active, even with network/ssh interrupts.
2. Resume previous shell from another location
3. Keep a long running process

i). Start named screen, I will explain why we need a name in later step.

screen -R <name> 
Eg : - screen -R udara

If you don't have screen installed on your system run,

sudo apt-get install screen

ii). De-attach from the current screen

ctrl+a+d , this will return you back to the default shell( where you start screen)

This is the interesting and powerful feature of shell, where you can De-attach or Re-attach to the same screen.

iii). Re-attach to the same screen

screen -r will re-attach you to the previous screen.

But let's assume we have multiple De-attached screens, screen -r will list down all existing screens in that case.

Eg :-

udara@udara-home:~$ screen -r
There are several suitable screens on:
    5192.udara    (2014-11-25 10:41:15)    (Detached)
    5135.JMeter    (2014-11-25 10:40:59)    (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.


This is where screen name comes handy :) You can easily recognize which screen you need to Re-attach. So use a meaningful name when starting your screen.

iv). Re-attach to a screen, when there are multiple De-attached screens

screen -r <screen-name>
Eg :-  screen -r 5192.udara

v). List existing screens 
Yes, If you have multiple screens screen -r will list down all existing screens. But there is a specific list command to do this.

screen -ls
screen -list


udara@udara-home:~$ screen -ls
There are screens on:
    5410.pts-7.udara-home    (2014-11-25 10:51:20)    (Detached)
    5192.udara    (2014-11-25 10:41:15)    (Detached)
    5135.JMeter    (2014-11-25 10:40:59)    (Detached)
3 Sockets in /var/run/screen/S-udara.



These are some basic commands which we can use to make our life easy, refer screen man page for more commands/shortcuts.

No comments:

Post a Comment