Wednesday, December 19, 2012

Command-Line Multitasking with Screen

 Most modern Unix like  operating systems (e.g. Linux, MacOS X, and BSD) come with ( or  can be installed )  a little console-mode utility called GNU Screen.  Even some of the power user who work exclusively on console are unaware of its existence.
What is screen ?
Screen is best described as a terminal multiplexer.  That is any number of console applications can be run concurrently with in a single terminal and you can quickly switch back and forth between these applications. If you are logged to a remote server via ssh , you can run many applications in  a single ssh session. The programs will be running even if you accidentally close the ssh connection.\

Starting  screen: launching and switching between programs
Start screen just by typing screen at your favourite command shell prompt. You'll probably be greeted by a welcome message. Dismiss this and you'll have with an empty terminal containing a shell prompt, which is pretty much what you had before you started screen.

        Every program running under screen runs in a window, and every window is identified by a unique number. Screen made a new window, numbered it 0, and started a command shell inside it.

Let us run ls command in this window or any other command so that we can recognise the window later on. 

Now make another window; this will be window 1.

To do this, type C-a c; that is, type Ctrl-a and then type c

Now that you have two windows,

. To switch between windows , type C-a C-a, which will switch you to whichever window you were using before the current one. Here are some more window  switching key sequences  if you have many windows open under screen.

  1. Use C-a n and C-a p to switch to the next or previous window in the list, by number.
  2. Use C-a N, where N is a number from 0 to 9, to switch to the corresponding window.
  3. Use C-a " to get a full-screen list of windows. You can navigate this list with the arrow keys (or vi-style, with j and k), and pick a window to activate by pressing Enter when it's highlighted. C-a w will give you a small, non-interactive list of windows.
 A window can be given a name if you type  C-a A    This name will be used in the window listing, and will help you remember what you're doing in each window when you start using a lot of windows.
Exiting the last program in a window will cause the window to disappear. You can also kill misbehaving programs with C-a K.

  Terminal decoupling and re coupling
   You can detach from  the screen session by pressing C-a d.  This means the screen session will be decoupled from the   terminal in which you started it up.  If  you close the terminal in which screen is running , the session will be automatically detached.   Detaching a screen session does not terminate any of the programs running in that screen session .
    Start up a new terminal emulator, and type screen -r  . You'll be right back where you were when you detached.
   You can detach from a screen session at work, shell into the machine from home, and reattach. 

 Copying, pasting, and the scrollback buffer

Screen remembers a configurable number of scrollback lines, and you'll need them because you won't be able to use your terminal emulator's scroll features while running screen. You can access the scrollback buffer by entering "copy mode", which is accomplished by typing C-a [. You can mark text anywhere in the scrollback buffer and paste it with C-a ]

Monitoring for silence and activity
One of the disadvantages of running a bunch of programs within screen is that you can't keep an eye on all of them at the same time, since in general you can only see one at once. ) To help mitigate this problem, Screen has features that allow you to monitor a window for silence--useful for knowing when a compile has finished, for example--or activity--useful for knowing when someone is finally talking on your favourite IRC channel, for example.
To start or stop monitoring the current window for 30 seconds of silence, type C-a _; to start or stop monitoring a window for activity, type C-a M.
Config Files
   Screen looks for a startup configuration file named .screenrc in your home directory.  Look in screen man pages for options.

No comments: