Linux/screen

From Omnia
(Redirected from Screen)
Jump to navigation Jump to 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 ...

Split Screen

To create a horizontal split, press Ctrl+A and then S (not the uppercase)

Ctrl-a S

To create a vertical split:

Ctrl-a | (pipe)

Move to other split pane:

Ctrl-a [tab]

Close current split:

Ctrl-a X
Ctrl-a :remove

Close all splits:

Ctrl-a Q

ref [2] [3]

CR/LF Mode

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

Session Name Management

Create a session with name:

screen -S SOMENAME

Join session with name:

screen -x SOMENAME

List sessions:

screen -ls

Rename session: [1]

screen -S [SESSION_ID] -X sessionname NEWNAME
# if only one session:
screen -X sessionname NEWNAME

Rename session in screen: [2]

C-a :sessionname NEWNAME

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" [4]

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): [5]

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." [6]

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

# set default title
shelltitle "."

# set hard line status
hardstatus alwayslastline
# hardstatus string "%{= KW} %H [%`] %{= Kw}|%{-} %-Lw%{= bW}%n%f %t%{-}%+Lw %=%C%a %Y-%M-%d"
hardstatus string "%{= KW} %H %{= Kw}|%{-} %-w%{= bW}%n%f %t%{-}%+Lw %=%C%a"


# --- default windows ---

# 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...

Hard Status Line

Screen User’s Manual: Hardware Status Line https://www.gnu.org/software/screen/manual/html_node/Hardware-Status-Line.html

-

Understanding GNU Screen's hardstatus strings · kbps https://www.kilobitspersecond.com/2014/02/10/understanding-gnu-screens-hardstatus-strings/

hardstatus string "%{= KW} %H [%`] %{= Kw}|%{-} %-Lw%{= bW}%n%f %t%{-}%+Lw %=%C%a %Y-%M-%d"
hardstatus string "%{= KW} %H [%`] %{= Kw}|%{-} %-Lw%{= bW}%n%f %t%{-}%+Lw %=%C%a %Y-%M-%d"
#
# http://www.gnu.org/software/screen/manual/html_node/String-Escapes.html
#
# %{= wK} : set colors to bright white (W) on bright black (K) and keep current text styles (=)
# %H      : hostname
# [       : opening bracket character
# %`      : print output of 'backtick' command (defined elsewhere in .screenrc)
# ]       : closing bracket character
# %{= wW} : set colors to white (w) on bright black (K) and keep current text styles (=)
# |       : bar character
# ${-}    : restore colors to previous colors / undo last color change
# %-Lw    : list windows before current window (L [optional] = "include flags")
# %{= bW} : set colors to bright white (W) on blue (b) and keep current text styles (=)
# %f      : window flags
# %t      : window title
# %{-}    : restore colors to previous colors / undo last color change
# %+Lw    : list windows after current window (L [optional] = "include flags")
# %=      : expand to fill all space (used here to make remaining content flush right)
# %C      : current time (12-hr; 24-hr is %c)
# %a      : am/pm (lowercase; uppercase is %A)
# %Y      : current year
# -       : hyphen character
# %m      : current month (0-padded; %M for "Jan" etc.)
# -       : hyphen character
# %d      : current date (0-padded)

hardstatus.png

backtick 0 30 30 sh -c 'screen -ls | grep --color=no -o "$PPID[^[:space:]]*"'

---

ubuntu - GNU screen status bar - how to make it display shell session names? - Unix & Linux Stack Exchange https://unix.stackexchange.com/questions/114505/gnu-screen-status-bar-how-to-make-it-display-shell-session-names

autodetach on 
startup_message off 
hardstatus alwayslastline 
shelltitle 'bash'

hardstatus string '%{wk}%?%-Lw%?%{=b kR}(%{W}%n*%f %t%?(%u)%?%{=b kR})%{= w}%?%+Lw%?%? %{g}]'

--- Caption Always ---

Might be better:

caption always "%{= kw}%-w%{= gW}%n %t%{-}%+w %-= bigdatums.net - %Y-%m-%d %C:%s"

ref: How to Display Window Tabs in Linux Screen - Big Datums - http://bigdatums.net/2017/03/25/how-to-display-window-tabs-in-linux-screen/

caption always – The caption command controls the display of window captions (as a line at the bottom of the screen)
%{= kw} – Clear all current attributes, set background to black, text to white
%-w – Display all window numbers and names up until the current window
%{= gW} – Set current window attributes, set background to green, set text to bright white
%n – Add number to current window
%t – Add name to current window
%{-} – Undoes the last attributes change
%-= – Set padding of caption string to fill terminal width
bigdatums.net – Example of using custom text
%Y-%m-%d %C:%s – Display the current date and time

... never mind, it is not.

---

tabs when using 'screen' - Unix & Linux Stack Exchange https://unix.stackexchange.com/questions/26248/tabs-when-using-screen/319364

# skip the startup message
startup_message off

# go to home dir
chdir

# Automatically detach on hangup. 
autodetach on

# Change default scrollback value for new windows
defscrollback 10000

# start with visual bell as default
vbell on
vbell_msg "bell on %t (%n)"

# look and feel
caption always "%{= bb}%{+b w}%n %t %h %=%l %H %c"
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"

activity "Activity in %t(%n)"

shelltitle "shell"
shell -$SHELL

--- prompt command ---

if [ 'screen' == "${TERM}" ]; then
  export PROMPT_COMMAND='printf "\e]2;%s %s\a" "${USER}" "${PWD}" '
fi

ref: tabs when using 'screen' - Unix & Linux Stack Exchange - https://unix.stackexchange.com/questions/26248/tabs-when-using-screen/319364

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
screen -d -m /usr/share/game/startlss.sh &

References:

keywords