2009-05-22

Start multiple command line screen session to run interactive jobs

As an BSD & Linux administrator, the command line is where we live. We eat, live & shit at the same place, command line screen. Some times, we can only afford to open a single terminal screen. It's kind of hard for us to be efficient as most of the time we multitask on a few bunch of screens.

Or, we need to run an interactive program over a long period of time which we want the result when it ends. Instead passing the program to run in the background which hides the result, the screen command is at disposal.

And most importantly, when combine with ssh (secure shell) it is uncomparablely fast and secure.

Screen is capable to detach from command line session and yet the program runs interactively in the background. The ability to switch from multiple screen will definitely help out when using the console session. Screen can even start in a few "screen session" in order to manage your task into separate groups. Screen also supports hot key (the meta control key with the combination of alphabet or digit) for common task.

Part I will guide any first time screen user on how to start a multiple screen command line to run multiple screen session. Proceed to Part II for a more advanced experience.

Part I - Browsing around
1. "S"tart screen by giving it a name
e.g.
screen -S dailytask
(co-incidently the parameter is the capital S alphabet)

2. The first screen presented is "num" 0. "C"reate another screen using hot key :
Ctrl-a then "c"

3. The second screen presented is "num" 1. Switch back to the "p"revious screen by :
Ctrl-a then "p"

4.The current screen is at "num" 0. Go to the "n"ext screen by :
Ctrl-a then "n"

5. If you are confuse, list the available screen in the current screen session by :
  • Ctrl-a then "
    (literally double quotes)
  • use the arrow keys up & down to browse through the list and press enter to choose the screen

6. Alternatively, switch the screen to "a"nother screen or vice-versa by :
Ctrl-a then a

7. Create more screens by repeating step 2, so that we simulate how to jump to the screen we want by :
  • Ctrl-a then <single digit from 0 - 9>
  • e.g.
    Ctrl-a then 2
    would jump to screen 2
  • The first screen starts from "0" and the 10th screen is "9"

8. To "d"etach the current screen session and allow the commands to run interactively :
Ctrl-a then d

9. The current screen is the main terminal session that we can logout or create another screen session. The screen session that we just detach will continue to run in the background, interactively.

10. To attach back the previous screen session, "l"ist the screen session :
screen -list

11. Then "r"e-attach the screen session :
screen -r dailytask


Part II - Essential commands

Now that you have an idea how screen works, we will go through some useful functions to make yourself more comfortable with the powerful screen utility.

- use the part I, step 5, list the screens. Choose the first screen and "a"nnotate it a screen name :
  • Ctrl-a then A
    (is a capital A, shift a)
  • remove the default name and named it "First screen"
  • jump or browse to other screen and repeat the hot key
    Ctrl-a then A
    to give it a name so to better differentiate screens

- copy and paste operation on screen
  • start the "copy" mode by
    Ctrl-a then [
  • move the cursor using the arrow keys (up, down, left, right) and press enter to start copy operation
  • use the arrow keys to highlight the desirable text to copy and press enter (again) to copy it
  • to paste,
    Ctrl-a then ]
    For testing, use vim to paste the text in insert mode.
  • to abort a copy or paste operation, press
    Esc
    key.
  • in "copy" mode, it works like vim, the editor. w or b or e=jump to the next word, gg=the very first line in the buffer, G=last line, |=move to the first character of the line, Ctrl-b=scroll up, Ctrl-f=scroll down, and lastly /=vim like search, n=find next and N=find previous

- "s"plitting screen into half top and half bottom
  • to start the screen splitting :
    Ctrl-a then S
  • switch to the next screen, in split :
    Ctrl-a then Tab
  • (literally double quotes) list and choose the screen to display on the split window :
    Ctrl-a then "
  • kill the current split window :
    Ctrl-a then X
  • quit the split screen and restore to full screen mode :
    Ctrl-a then Q
- Scroll back / browse the history of screen
Ctrl-a then [
(equivalent to copy mode) then use arrow keys (up, down, left, right) to browse - Since screen can be re-attach, it is not secure. Lock it with password protected prompt :
    Ctrl-a then x
  • key in a password, and repeat it. Do not forget it, it is unrecoverable.
- Misc.
  • should a screen stuck and cannot be "r"e-attach, "d"etach it first then re-attach it back. e.g.
    screen -rd dailytask
  • some times screen are stuck with infinity loops or stubborn programs that refuse to end itself. "k"ill it. e.g.
    screen -k 12345.daily
  • Increase the scroll back buffer / history (default is 100 lines only!!!)

    1. Go to screen's command mode by :
      Ctrl-a then colon
      (colon is ":")
    2. type the below and press enter:
      scrollback 2000
    3. use the the below command to check the buffer settings :
      Ctrl-a then i
AdiĆ³s !!!

No comments: