Thursday, December 18, 2014

Quick Start to GNU Screen


Today I sought out to learn screen. So I started by looking at the screen manual to get a good understanding of how the tool worked.

While I was able to get what I feel are the basics down from reviewing the manual, I did not feel that the documentation provided enough information to get you off the ground quickly.

As the aim of this is to be a quick start, I will start with at tl;dr version, and then go into details on how I build out this list.

In short from this list you should be able to get started using screen and build out a productive work environment using a single TTY terminal.

Start Screen: screen
Show Displays: screen -ls
New Window: C-a C-c
Show Window List: C-a "
Switch to Window: C-a ' NUMBER
Next Window: C-a n
Previous Window: C-a p 
Close Window: (Kill Shell - exit) or C-a k (not recommended)
Name a Window: C-a A

Detach from Display: C-a d
Reattach to Display: screen -r NAME

Create Region Horizontal: C-a S
Create Region Vertical: C-a |
Close current Region: C-a X

Send a command to a Window: at NUMBER# stuff "COMMAND\r"
Send a command to all Windows: at \# stuff "COMMAND\r"




The first step to learning screen (outside of how to start the program) is how to get the program to do what you want. The key to this is a key binding known as C-a, and what key stroke does is tells screen to listen and intercept other keys as commands, or flags to alter the modes (similar to vim modes) in which screen runs.

- Note: to see if screen in running or how many screens your attached to run the following:

$ --> screen -list
There is a screen on:
        3664.pts-2.HOSTNAME     (Attached)
1 Socket in /var/run/screen/S-USER.



With this understood the next thing to know is how to type the C-a keystroke. C-a is really Ctrl + the 'a' key, and C-a is simply just the short hand notion for this key stroke.

Simply typing the keystroke does not thing unless its paired with other keystrokes (key bindings), of which well talk more about later (as these other key strokes are key to using screen), however its important to know how to interact or start an interaction with the screen utility and not interfere with the the terminal your running.

With an understanding of how to get the screen utilities attention its time to see how screen does what it does. The simplest way to see this is to use the following:


From this picture you can see that display are created and these directly map to tty sessions on the system you can see this with C-a * and who (the system command). 

C-a *
term-type   size         user interface           window       Perms
---------- ------- ---------- ----------------- ----------     -----
 xterm-256c 211x33       USER@/dev/pts/2          0(bash)       rwx

$ --> who
USER    :0           2014-12-18 06:55 (:0)
USER    pts/2        2014-12-18 06:59 (:0)
USER    pts/3        2014-12-18 06:59 (:pts/2:S.0)
USER    pts/5        2014-12-18 07:05 (:pts/2:S.2)

In the examples above you can see that I have 1 display and that its currently viewing window 0 (named bash). From the who output you can see I have another window (2) however it resides in the background. 

This is cool because it allows you to more that one thing at a time because you can switch between windows running different terminals.

To show you how you get to this point simply start screen, then run C-a C-c. The C-c command is what creates a new window. Because this can be done while you are running other tasks the current window is/does not need to be interrupted to get to terminal where you can continue working.

In addition to using windows for this you can use the screen display to do the same task.

Example:

$ --> screen
$ --> #start long running process
.....C-a d
## This detaches you from the screen display. 
$ --> screen -ls  ## -ls is short hand for -list
There is a screen on:
        3664.pts-2.HOSTNAME     (Detached)

...

The nice part about his is that it allows for you to do thing with one terminal that would often require that you back ground process (and loose output to standard out and standard error).

An added benefit to this is that you can share your screen or display. In short multiple users can use screen to attach to a display and from there view the windows that that display is managing.

Screen can also be used to setup a work environment (dual terminals) to you can work and review data at the same time. This is accomplished by using regions a consent that resides with in a display to separate the display in to segments. Using C-a S will create a horizontal split in your window and allow us to use C-a <tab> to move between the regions, and just like the horizontal split a vertical split can be created with C-a |. To then close or remove the region you can run C-a X.



One draw back to using screen is that you often loose your ability to scroll (with a mouse). Often this is frustrating for new users because long running tasks often have lots of long running output. To deal with this screen has a scroll feature that can be enable with C-a [. Once enabled you are given vim key bindings to move around the window.

- Note: for convince you can use C-u and C-d to scroll by half pages.

Its the little things (like opening a web browser)

Sometimes as your developing a solution/script for a problem your faced with interesting challenges where the dumbest workaround (opening a ...