I Don't Know How I Lived Without Screen

From UCLUG
Jump to: navigation, search

This is not my talk yet, just fleshing it out. Work in progress. Move along, move along... chruck 15:26, 4 September 2009 (UTC)

discuss Contents, "Here's what I use"

Concepts[edit]

What Screen Is[edit]

  • Face it: Linux: must use commandline
  • "Window Manager"
  • Allows reconnecting to sessions
  • Not invasive: Gets out of the way
  • Multiple windows on the same virtual terminal
  • Multiple access to the same virtual terminal

Nominclature[edit]

terminal 
display that shows standard output from computer, usually text <ALT>-F1
virtual terminal (vt) 
terminal that may/not be attached to display <ALT>-Fx
X terminal (xterm) 
X window program that displays vt, eg xterm, Kterm, Gnome-terminal, rxvt, Eterm
attach 
session in current terminal
detach 
session running in background on vt
window 
process (usually shell) running on its own vt
session 
an instance of screen

Use Cases[edit]

discuss later?

Commandline Options[edit]

-ls -list 
list screen sockets
-d [label] 
detach
-r [label] 
reattach
-DR [label] 
detach/reattach force (create if not existing)
-x 
multipass [label]
-wipe 
clean up socket list
-S [label] 
label screen session

Concretes[edit]

Key Bindings for Commands[edit]

screen

  • <CTRL>-A → screen's command prefix. All the following need <CTRL>-A prepended:
 ? 
help
detach
create a window echo zeroth, echo first, second, third
<SPACE>, n 
next window, wraps around
prev window, wraps around
<CTRL>-A 
last window
0 to 9 
jump to that window number first → third
send <CTRL>-A to vt vim
window title
show windows
goto window from list
split
<TAB> 
focus
<ESC> 
begin copy mode (view scrollback)
paste

Copy Mode[edit]

  • vi-like movement, searching
  • <SPACE> marks what to copy
  • <ESC> exits

.screenrc[edit]

# Have 1000 lines buffered to go back to via C-a esc  (-h)
defscrollback 10000
# On the OLPC, the 'erase' key becomes 'NUL'.  This fixes:
bindkey -d "\000" stuff "\010" 
caption always
screen su -
screen mutt
screen less +F /var/log/messages
screen

Conclusions[edit]

Gotchas[edit]

  • $DISPLAY
  • $TERM
  • screen within screen
  • other programs that use <CTRL>-A (minicom, vim)
  • <BEL> → vbell
  • multi-display on different-sized vt

Resources[edit]

  • <CTRL>-A ?
  • man screen
  • info screen
  • google
  • uclug
  • Eric

Newbie Questions[edit]

Your turn to ask

Guru Hints[edit]

Auto-reconnection script[edit]

The following script attempts to reconnect you to the last screen session that your remote IP started via SSH. If multiple exist, it reconnects you to the first available session. If none are available, it creates a new session. Just stick this in your ~/.bashrc file:

if [ -n "$SSH_CONNECTION" ] && [ -z "$SCREEN_EXIST" ] && [ -z "$BASH_EXECUTION_STRING" ]; then
    export SCREEN_EXIST=1
    client_id=`echo $SSH_CLIENT | awk '{ print $1 }'`
    count=`screen -ls | grep "$client_id" | grep '(Detached)' | wc -l`
    # Check to see if there's an existing session from this IP
    if [ $count -gt 0 ]; then
      session_id=`screen -ls | grep "$client_id" | grep '(Detached)' | head -1 | awk -F'.' '{ print $1 }'`
      screen -DR $session_id
    else
      screen -R $client_id
    fi
    # Uncomment this line if you want your SSH session to disconnect when you exit the screen session
    # However, if this script has bugs...you may just lock yourself out of your machine, short of a live CD...
    #exit
fi