Friday, October 19, 2012

Using Screen in Ubuntu

This morning I was advised that in order to execute extensive shell scripts, and especially via telnet, I should look into using screen.

When I first tried the command screen in Ubuntu I found that it didn't work, I was notified by bash that I should sudo apt-get install screen. So, after I installed it as bash suggested, I re-executed the command screen and a GNU lic agreement was displayed. Then I was placed back on to a command prompt, almost exactly the same command prompt as what I was on before... so what next ?

I decided to do a bit of research into screen, from what I was advised that it is a essential tool that should be used my most people but in reality it is overlooked. So if it is so important why is there a lack of understanding of screen?

Well here is what I found out: When you are using a terminal window or if you are using telnet, you are assigned a session. Within the session you can execute all the commands that you need as long as the session is still active. If the session is terminated then your commands exit. So how does your session terminate ? well if you close down your terminal (accidently or not) or if you close your telnet session (accidently, forced, or via another method).

So, if you are working on a complex build and it is taking a long time you are forced to keep the session open until the build completes. A complex build is problematic with a temperamental telnet session. Here is where screen comes in to the rescue.

Screen detaches your session, and hence allowing your program to continue to execute even when the terminal has been closed or your telnet has been closed. You can regain the session easily by re-establishing a terminal window or telnet session and re-executing screen.

Sounds handy huh?

Starting Screen
To start screen you can:

  1. Enter the command screen
  2. Enter the command screen -S <sessionname> where the <session name> is any name to describe the session. For example screen -S 1 and screen -S mySession are valid ways of describing the sessions. This comes in handy if you want to open multiple sessions, having a meaningful name means you can open the right session when you want it. 
Getting Back to Your Prompt
To exit the session you need to a key combination :

CTRL + A and then CTRL + D

To try this first execute screen, then execute a command that you would notice that you have only executed in screen such ls -a or pwd  etc... then try CTRL + A and then CTRL + D or CTRL + A +D
Did you notice how you were returned back to your original command prompt ? 

Getting Back to Your Screen Session
To restart your previous screen session you can:
  1. Enter the command screen -r if you started screen without a session name then just use this command. 
  2. Enter the command with a session name screen -r <session name> where <session name> is the name of the session that you started previously. For example screen -r 1 or screen -s mySession.

Starting Another Screen Session Within A Session
If you want to start another screen session within a session, you don't need to CTRL + A CTRL + D and start another session with screen -S myNewSession. You can simply execute the following key combination:

CTRL + A and then CTRL + C

How To Navigate Between the Sessions
When you are working with multiple sessions you will want to navigate between the sessions. To do this you can do the following:
  1. Next window : CTRL + A and then CTRL + N
  2. Previous window : CTRL + A and then CTRL + P
  3. Selecting from a list of windows : CTRL + A and then CTRL + "
  4. The last session that was viewed : CTRL + A and then CTRL + A

That's all the basics of Screen, there are plenty of other great commands. Let me know if you want a guide on the other commands. 

0 comments:

Post a Comment