Linux/screen

From Omnia
Jump to: navigation, search

Screen

screen - A screen manager that supports multiple logins on one terminal.

---

Screen User's Manual

List of Key Bindings and Commands

Screen tutorial

$ yum info screen

Summary: A screen manager that supports multiple logins on one terminal.

Description:
The screen utility allows you to have multiple logins on just one
terminal. Screen is useful for users who telnet into a machine or are
connected via a dumb terminal, but want to use more than just one
login.

installation

To install screen:

yum install screen

To start screen:

screen

Command Line Options

To create an unamed session:

screen

To create a named session

screen -S [SESSION_NAME]

To reattach as the controlling terminal (only one can be the controlling terminal):

screen -r
screen -r [[pid.]tty[.host]]
screen -r [SESSION_NAME]

When wanting to connect to a specific session, you can generally use just the 'pid' (instead of the full '[[pid.]tty[.host]]' format).

To attach to current session (multi view mode):

screen -x
screen -x [[pid.]tty[.host]]
screen -x [SESSION_NAME]                # join a named session
screen -x -p [WINDOW#] [SESSION_NAME]   # join a named session on WINDOW#

To list screen sessions:

# Sessions marked ‘detached’ can be resumed  with  "screen  -r".  Those marked
# ‘attached’  are running and have a controlling terminal. If the session runs
# in multiuser mode, it is marked ‘multi’.
screen -ls
screen -list

Start a screen session in detached mode:

# Start  screen  in  "detached" mode. This creates a new session but doesn’t attach
# to it. This is useful for system startup scripts.
screen -d -m
screen -d -m [INITIAL_COMMAND]
screen -d -m -S [SESSION_NAME] [INITIAL_COMMAND]

To stuff a string into a running session: (does not join session) [1]

screen -x [SESSION_NAME] -X stuff "[COMMAND]"
screen -x [SESSION_NAME] -p [WINDOW#] -X stuff "[STRING]"
screen -x [SESSION_NAME] -p [WINDOW#] -X stuff "[STRING]"`echo -ne '\015'`

To execute a command in a running session

screen -x [SESSION_NAME] -p [WINDOW#] -X exec "[COMMAND]" [PARAM1] [PARAM...]

To change the currently selected the current window: (does not join session)

screen -x [SESSION_NAME] -X select [WINDOW#]

Screen Command Keys

Help:

Ctrl-a ?

Enter command-line mode. (colon) (Command Summary):

Ctrl-a :

Pass 'ctrl-a' through to application:

Ctrl-a a

Session Management

To detach:

Ctrl+a d
"You can also detach just by closing the terminal emulator that contains the session. All of the programs you started running within screen are still running."

To kill a session, (or window if exists):

Ctrl+a K
exit

To quit screen and kill all windows:

Ctrl-a :quit

Windows

Create window:

Ctrl-a c  (mnemonic: create window)

List windows interactive:

Ctrl-a "
"to get a full-screen list of windows. You can navigate this list with the arrow keys (or vi-style, with j and k), and pick a window to activate by pressing Enter when it's highlighted."

Switch between current window and previous window:

Ctrl-a Ctrl-a  (mnemonic: alternate window)

Switch next/previous window:

Ctrl-a n  (mnemonic: next window)
Ctrl-a p  (mnemonic: previous window)

Switch to window # (0 is first screen):

Ctrl-a [0-9]  (mnemonic: window #)

List windows (with number and name):

Ctrl-a w
"will give you a small, non-interactive list of windows"

Show the number (and title) of the current window

Ctrl-a N

Name a window:

Ctrl-a A <name>

Switch to a window by name or number:

Ctrl-a ' <name_or_number>

To kill a window:

Ctrl+a K  (mnemonic: kill window)
exit

Renumber a window (switch if already exist):

Ctrl-a :number [number][enter]

Pass Ctrl-a to application: (good for screen in screen)

Ctrl-a a ...

CR/LF Mode

"Ctlr-A U" allows me to change CR/LF mode

Scrollback

"Screen remembers a configurable number of scrollback lines, and you'll need them because you won't be able to use your terminal emulator's scroll features while running screen" [2]

To scroll back through screen's history, first enable the copy mode by pressing Ctrl+a [ . Now, use either one of the arrow keys, page up, page down, home and end keys to scroll through screen's history.

Pressing Enter will start marking text for copying. Pressing Enter again will copy the selected text into the clipboard. Now, to paste that text, press Ctrl+a ].

# Scroll back
Ctrl+a [

# Move around
[Arrow Keys]

# Mark begin
Enter

# Copy text from begin to here
Enter

# Paste text
Ctrl+a ]

# Search forward
/[TEXT]

# Search backwards
?[TEXT]

# Search again (in same previous direction)
n

# Dump current window scrollback to a file
Ctrl+a :hardcopy -h <filename>

# Set copy buffer file:
Ctrl+a :bufferfile <filename>
# ...
# Write copy buffer to file:
Ctrl+a >

Clear scrollback (per window): [3]

Ctrl+a :scrollback 0
Ctrl+a :scrollback 15000
# or bind this to a key, such as Ctrl+a C, as that is a useless clear screen key!
bind C eval "scrollback 0" "scrollback 15000"

By default, the scrollback buffer only keeps the last 100 lines of text, which is not enough for a typical interaction with Screen. I’ve found a setting of 5000 lines to be more than adequate for my usage. The number of scrollback lines can be configured in your $HOME/.screenrc file, by adding the following line:

# $HOME/.screenrc
defscrollback 5000

References:

Monitor Window Activity

Monitor the current window for 30 seconds of silence:

Ctrl-a _

To start or stop monitoring a window for activity

Ctrl-a M

Exit Screen

"As you've seen from the section on detaching and reattaching, screen is not easy to kill. You can't just close its containing terminal. (Actually, you can, but then all those processes are still running on your system, ready to be reattached.) There's no "quit" key.
How do you fully exit screen?
To my knowledge, the only way to do this nicely (i.e. without sending nasty signals to processes that don't deserve it) is to close each of your screen windows. Exit whatever programs or shells each is running, and they will go away. When the last program running inside screen is exited, screen itself will go away." [4]

According to the screen man page (version 4.00.03):

C-a C-\     (quit)        Kill all windows and terminate screen.

The "C-a C-\" character does not appear to be captured. Another solution is to send the 'quit' command.

To send a command:

C-a : <command>

To quit:

C-a : quit

Log to file

C-a H  # start/stop logging to screenlog.N
"You can also use Control-a + H to save loggings into screenlog.n file. One more Control-a + H to turn off. C-a H: Begins/ends logging of the current window to the file "screenlog.n"."

logging - Save "Screen" (program) output to a file - Stack Overflow - http://stackoverflow.com/questions/14208001/save-screen-program-output-to-a-file

nested screen

To control a screen within a screen, just append the following:

[ctrl]+[a] [a] ...  # sends a [ctrl]+[a]

Create new window:

[ctrl]+[a] [a] c

.screenrc

defscrollback
Same as the scrollback command except that the default setting for new windows is changed. Initial setting is 100.
# Kenneth's Screen Configuration - $HOME/.screenrc

# Auto detach on terminal close - default setting is on
autodetach on

# Hide startup copyright screen
startup_message off

# 10,000 lines of scrollback!
defscrollback 10000

# flash screen on bell
vbell on

# clear scrollback with '[ctrl]+a c'
bind C eval "scrollback 0" "scrollback 15000"

# set default terminal - default is screen.xterm
# alternative # term xterm
term linux


# default windows
screen -t local

# run 'mutt' program
#screen -t mail  mutt

# default push
#screen -t root
#stuff "sudo su -\015 cd /\015"

#screen -t 'my tasks'
#stuff "cd /esx\015"

Screen articles to read

The Dungeon Collapses

If you kill screen:

$ kill [pid]
Suddenly the Dungeon collapses!! - You die...

Issues

Must be connected to a terminal

When trying to start screen from a startup script, or non login session:

Error:

Must be connected to a terminal.

Example:

ssh root@my.machine screen "tail -f /var/log/messages"

Solution:

"ssh -t ..." will force pseudo-tty allocation.

Solution:

"screen -d -m" will start in detached mode

References:

keywords