Thursday, November 01, 2012

Screen way of terminal management

What is screen?

It is  a terminal multiplexer, essentially giving the ability . man screen is the best option still.

Getting started video can be useful.

Why screen?

Its original reason for existence was allowing you to switch between sub-sessions on a video-display terminal, but it grew to allow sessions that could be detached and reattached (if you went home for the day, or say you were connecting via a glitchy network) and eventually to allow the same session to be simultaneously accessed from multiple places.

Real power of screen


  1. Persistence of the terminal based work. 
  2. Start your builds -- go home and see all the results intact. Resistant to laptop or network snags.
  3. Unlimited windows (unlike the hardcoded number of Linux virtual consoles)
  4. Scroll-back buffer (not limited to video memory like Linux virtual consoles) & Logging.
  5. Copy/paste between windows.
  6. Notification of either activity or inactivity in a window
  7. Split terminal (horizontally and vertically) into multiple regions
  8. Locking other users out of terminal.



Advanced users of screen

Buffering

Ctrl +a+ [  --------- Page or arrow keys work best
Ctrl +a+ ]  --------- To close the buffer read session

Helpful on remote machines where the logs are being tail-ed.

Terminal Scrolling

Though the above buffering solution of (Ctrl+a+[ ) helps, screen uses alternate screen. So one way is to tell screen not to use this. Scroll-back can also be achieved on the terminal using
‘termcapinfo xterm ti@:te@’ to your .screenrc file.

Starting and Naming sessions


screen -S  

Reconnecting to screen session


screen -x

Split Screen


You can split your terminal windows horizontally and vertically (for vertical split, you need to patch your screen).
To Split the screen horizontally: Ctrl+a & S
To split the screen vertically: Ctrl+a & | or Ctrl+a & V ( the screen has to be patched for this)
To switch between windows: Ctrl+a & Tab
To kill your current window: Ctrl+a & X

Logging


The best thing what I like about screen is that you can have logs of anything you have done on screen, which can be used in lot of things in future. To enable logging, use “Ctrl+a” followed by “H“. This will create a file with name “screenlog.0” in your home directory, which will contain anything or everything you have done on the screen. If you want to stop logging, use the same, “Ctrl+a” & “H“.
You can also enable logging while starting your screen session, which can be done by using “-L” switch:
screen -L -S

Copying and Pasting across terminals 

To enter copy mode in screen, hit Ctrl+A, then [. You can now use the arrow keys, or vim-like keybindings, to move around the screen. 0 gets you to the start of a line, and $ to the end of a line.

  • When you've reached the point where you want to start the copy, hit Enter.
  • Move the cursor to the end point of your selection (you'll see what you're copying highlighted), and then hit Enter again.
  • Now move the cursor to wherever you want to paste the selection (you can move to another screen within your session), and hit Ctrl+A ] to paste the selection.
  • You can also use Ctrl+A > filename to paste the selection to a filename and Ctrl+A < filename to read a file into the selection buffer so you can then paste it out again using Ctrl+A ].

When in copy mode, you can also use screen's scrollback feature (i.e., you can scroll back upward past the top of the currently displayed text) and the search function.

Command line Ctrl+a does not work in screen?

It works in a different way where ctrl+a+a will get the effect.

Searching in the buffer (Like VIM )

When in Buffer mode (Ctrl + [) use / for backword search and ? for forward search. 

Getting ..... lines on the screen terminal

You see this when the user is connected in a multi-user mode and 2 users are on the same terminal. To overcome this, first issue screen -d and then connect using screen -x  

Increasing the scroll back limit

defscrollback 1000
This increases the scrollback to 1,000 lines; you can edit this value as you prefer.

Cheat-Sheet for screen


Command         Description
Ctrl+a, ?       Show built in help
Ctrl+a, c       Create a new screen
Ctrl+a, K       Kill a screen session.
Ctrl+a, Ctrl+a  Cycle through screens (just hold Ctrl down, type aa)
Ctrl+a, n       Cycle through screens (Next screen)
Ctrl+a, Ctrl+n  Another cycle through screens
Ctrl+a, S       Split the screen (note the capital S)
Ctrl+a, X       Close the split screen you are currently in (note capital X)
Ctrl+a, x       lock a screen
Ctrl+a, tab     Switch between split screens
Ctrl+a, d       Detach from screen session
Ctrl+a, [       Start copy 
Ctrl+a, ]       Paste
Ctrl+a, W       Status
Ctrl+a, H       Start Logging, Repress to stop logging

{Update} 

Getting in

start a new screen session with session namescreen -S 
list running sessions/screensscreen -ls
attach to a running sessionscreen -r
… to session with namescreen -r 
the “ultimate attach”screen -dRR (Attaches to a screen session. If the session is attached elsewhere, detaches that other display. If no session exists, creates one. If multiple sessions exist, uses the first one.)

Escape key

All screen commands are prefixed by an escape key, by default C-a (that's Control-a, sometimes written ^a). To send a literal C-a to the programs in screen, use C-a a.

Getting out

detachC-a d
detach and logout (quick exit)C-a D D
exit screen“C-a : quit” or exit all of the programs in screen.
force-exit screenC-a C-\ (not recommended)

Help

See helpC-a ? (lists keybindings)

Scripting

send a command to a named sessionscreen -S  -X 
create a new window and run ping example.comscreen -S  -X screen ping example.com
stuff characters into the input buffer using bash to expand a newline character (from here)
screen -S <name> [-p <page>] -X stuff $'quit\r'
a full example
# run bash within screen
screen -AmdS bash_shell bash
# run top within that bash session
screen -S bash_shell -p 0 -X stuff $'top\r'
 
# ... some time later
 
# stuff 'q' to tell top to quit
screen -S bash_shell -X stuff 'q'
# stuff 'exit\n' to exit bash session
screen -S bash_shell -X stuff $'exit\r'

Misc

redraw windowC-a C-l
enter copy modeC-a [ or C-a  (also used for viewing scrollback buffer)
pasteC-a ]
monitor window for activityC-a M
monitor window for silenceC-a _
enter digraph (for producing non-ASCII characters)C-a C-v
lock (password protect) displayC-a x
enter screen commandC-a :
Interesting usage of screen. 
Reference: 
They are using it for running the server in the background. 
Good collection of links at the end 


No comments: