Categories
Bash Open Source Tools Unix Admin

screen : Run processes on re-attachable virtual terminals

On unix a utility called “screen” provides a quick and easy way to run commands on virtual terminals which can be re-attached later as and when needed even when user is connecting remotely.

Many a times I am doing production support from my Blackberry or have logged in on VPN which could get disconnected for one reason or another. Then there are times when I am working on some fixes and it is time to go home but I want to continue working from home later on the same task. The problem that all of these have in common is that all of your old sessions were running on the local system console which you no longer can access remotely. Some would say use a remote desktop client (like VNC, RDP etc) which would work but not only they are CPU intensive but worst of all Bandwidth consuming. You cannot do that on Blackberry or from a tethered connection on the road. So you need a text based solution with a very small bandwidth signature so it can be responsive enough for a tethered connection. Unix has a utility called screen which has been there for at least last 14 years (since I have been working on Unix systems. This was my first utility before vi that I started to love to use). Think of this as terminal multiplexer.

Quoted from GNU site (http://www.gnu.org/software/screen/)

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal and, in addition, several control functions from the ANSI X3.64 (ISO 6429) and ISO 2022 standards (e.g., insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows. When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, turn output logging on and off, copy text between windows, view the scrollback history, switch between windows, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the users terminal.

So basically, Screen provides the user the ability to allocate a virtual terminal which can be re-attached whenever needed. Think of a very advance nohup command which will allow something to run in background even when terminal is closed. Now nohup does not provide a way to re-attach to the application but screen does. Here are the most basic uses of “screen” that I use so often that these short cuts are hard-wired in my brain.

To invoke : screen

Short cuts while you are on the screen

To detach : CTRL+A+D
To create a new subshell on current screen: CTRL+A+C
To move to a different subshells: CTRL+A+0 (for first screen), CTRL+A+1 (for first screen) and so on (0..9)
To move to next subshell: CTRL+A+N
To move to previous subshell: CTRL+A+P
To move between current and last subshells: CTRL+A+A
To quit a subshell: Simply quit that subshell (CTRL+D or exit etc).

How to re-attach remotely

To reattach: screen -r (If there is only one detached screen, it will attach to it, otherwise it will list all active screens for the user). To attach to a particular screen run screen -r with id of the screen, e.g.

unixite@sandbox:> screen -r
There are screens on:
	26374.pts-1.sandbox	(05/30/2010 12:42:55 PM)	(Detached)
	18130.pts-1.sandbox	(06/12/2010 09:05:54 PM)	(Detached)
2 Sockets in /var/run/screen/S-unixite.

unixite@sandbox:> screen -r 18130

To detach remotely: When you are logged in remotely and found that you missed to detach a screen from console use screen -d.
To find all running screens: screen -list or screen -ls for short.

This post is still in works and will continue :-). So happy screening.