LINUX - screen/dtach - basic attach detach reattach - copy splitting - cheatsheet

screen and dtach allow to detach the current terminal from the shell. Thus you can turn off your shell (putty/teraterm) and still have your sessions running. 

You can start a session with putty connect to a server, start screen(or start dtach), start a long disk clone, close the putty(this will auto detach your screen or dtach session) and you will be okay the clone will still be going on, you can turn on putty on another computer just connect to the same server and go back to your session (reattach to your screen or dtach session). The clone will be going the whole time while your driving to that other computer.

DTACH STARTING DETACHING AND REATTACHING
###########################################

dtach is a common linux utility
apt-get install dtach

Similar process with detach:
dtach -A /tmp/session1 /bin/bash
To detach: Control-\ 
Processes will still be running in /tmp/session1 session file
To reattach: dtach -A /tmp/session1

SCREEN STARTING DETACHING AND REATTACHING
###########################################

apt-get install screen

Start Screen: screen
Better start screen: screen /bin/bash
Detach Screen: Control-a d
When detached all processes still are running
To reattach: screen -x
To reattach (substitute in the PID): screen -x PID
Before reattaching list all screen sessions: screen -ls

Scrolling on the pages
#####################
With normal system (Without screen or byobu):
SHIFT-PAGE UP
SHIFT-PAGE DOWN

Note: byobu is an enhanced version of screen, it gives you a task bar and tabs (along with screens split tab ability and also everything runs in the background so you can be on the go)

With byobu / screen
Control-a then [
Then PAGEUP and PAGEDOWN
Also UP and DOWN arrow keys work with BYOBU


SCREEN - How to copy/save output with screen
#############################################

This is similar to copy, because you will drag a box aroudn the output you want to save.

Control-a [ To start copy process
Use arrow keys to scroll cursor up down left right to start point Hit space (top left corner will be set
Scroll down with arrow keys to end point (bottom right corner will be set)

Control-a >
This will save the stuff you just copied to a file called /tmp/screen-exchange

SCREEN -  Log screen
#####################

Control-a h
takes a "screenshot" of the current text (only whats showing - nothing behind the "walls" - nothing you have to scroll up or down to)

Control-a H
log file dump to current working directory file wil lbe called screenlog.0

Control-a t
Shows system info, hostname, time, and load average

SCREEN -  Splitting and more
####################

Control-a S
Split screen horizontally

Control-a |
Split Screen Vertically

Control-a Tab
Move to other split window

Control-a c
Creates a new session might be usefull in split screen or in general

Control-a "
To see all sessions
Use UP and DOWN arrow keys and enter to select

Control-a :remove ENTER
Unsplit
Can bind in to a simpler key:
vi ~/.screenrc
Add this in:
bind X remove
exit and save
Then control-a X will work


#############################################
#############################################

Summary from my older screen article:

LINUX - screen - best commands and detaching and reattaching to session

SCREEN COMMANDS
################

These are the ones that I use the most.. These are awesome, write em down, remember em.

CONTROL-A then c - create new shell
CONTROL-A then S - split window with horizontal line ---
CONTROL-A then | - split window with vertical line |
CONTROL-A then TAB - move over to next window
CONTROL-A then " - see all windows
CONTROL-A then [ - copy mode (up down left right, page up page down move around in window)
CONTROL-A then X - close window
CONTROL-A then D - detach (processes still are running)
CONTROL-A then A - set name to window
CONTROL-A then ? - help

* Note all of these work with BYOBU if you allow BYOBU to use SCREEN commands, which I would

How to recover a 'lost' screen session?
#########################################

Somehow I managed to close a screen window without screen 'noticing' it, so the session is still flagged as attached. This prevents me from re-attaching to this session. What can I do?

$ screen -r

also

$ screen -ls

There are several suitable screens on:
    25028.pts-19.XXX-serv8  (01/05/2012 07:15:34 PM)    (Attached)
    24658.pts-19.XXX-serv8  (01/05/2012 07:11:38 PM)    (Detached)
    24509.pts-19.XXX-serv8  (01/05/2012 07:10:00 PM)    (Detached)
    18676.pts-5.XXX-serv8   (01/02/2012 06:55:33 PM)    (Attached)

#######################################################

Type "screen [-d] -r [pid.]tty.host" to resume one of them.

#######################################################

Try detaching it first with screen -d. If that doesn't work, you can try, in increasing order of emphasis,

   -d|-D [pid.tty.host]
        does  not  start screen, but detaches the elsewhere running screen session. It has the
        same effect as typing "C-a d" from screen's controlling terminal. -D is the equivalent
        to  the  power  detach key.  If no session can be detached, this option is ignored. In
        combination with the -r/-R option more powerful effects can be achieved:

   -d -r   Reattach a session and if necessary detach it first.

   -d -R   Reattach a session and if necessary detach or even create it first.

   -d -RR  Reattach a session and if necessary detach or create it. Use the first  session  if
           more than one session is available.

   -D -r   Reattach a session. If necessary detach and logout remotely first.

   -D -R   Attach  here and now. In detail this means: If a session is running, then reattach.
           If necessary detach and logout remotely first.  If it was not running create it and
           notify the user. This is the author's favorite.

   -D -RR  Attach here and now. Whatever that means, just do it.
   
#######################################################

SelectionFile type iconFile nameDescriptionSizeRevisionTimeUser
Comments