Linux/screen

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: screen -ls screen -list
 * 1) Sessions marked ‘detached’ can be resumed  with  "screen  -r".  Those marked
 * 2) ‘attached’  are running and have a controlling terminal. If the session runs
 * 3) in multiuser mode, it is marked ‘multi’.

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

To stuff a string into a running session: (does not join session) 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

Switch to a window by name or number: Ctrl-a ' 

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"

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

Ctrl+a [ [Arrow Keys] Enter Enter Ctrl+a ] /[TEXT] ?[TEXT] n Ctrl+a :hardcopy -h Ctrl+a :bufferfile Ctrl+a > Clear scrollback (per window): Ctrl+a :scrollback 0 Ctrl+a :scrollback 15000 bind C eval "scrollback 0" "scrollback 15000"
 * 1) Scroll back
 * 1) Move around
 * 1) Mark begin
 * 1) Copy text from begin to here
 * 1) Paste text
 * 1) Search forward
 * 1) Search backwards
 * 1) Search again (in same previous direction)
 * 1) Dump current window scrollback to a file
 * 1) Set copy buffer file:
 * 1) Write copy buffer to file:
 * 1) Write copy buffer to file:
 * 1) or bind this to a key, such as Ctrl+a C, as that is a useless clear screen key!

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: defscrollback 5000
 * 1) $HOME/.screenrc

References:
 * Copying GNU screen scrollback buffer to file (extended hardcopy)? - Stack Overflow - http://stackoverflow.com/questions/4807474/copying-gnu-screen-scrollback-buffer-to-file-extended-hardcopy
 * Screen - Copy and Paste - http://stuff.mit.edu/afs/athena.mit.edu/project/gnu/doc/html/screen_11.html#SEC72

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

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 :

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.


 * 1) Kenneth's Screen Configuration - $HOME/.screenrc

autodetach on
 * 1) Auto detach on terminal close - default setting is on

startup_message off
 * 1) Hide startup copyright screen

defscrollback 10000
 * 10,000 lines of scrollback!

vbell on
 * 1) flash screen on bell

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

term linux
 * 1) set default terminal - default is screen.xterm
 * 2) alternative # term xterm

shelltitle "."
 * 1) set default title

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


 * 1) --- default windows ---

screen -t local
 * 1) default windows


 * 1) run 'mutt' program
 * 2) screen -t mail mutt


 * 1) default push
 * 2) screen -t root
 * 3) stuff "sudo su -\015 cd /\015"


 * 1) screen -t 'my tasks'
 * 2) stuff "cd /esx\015"

Screen articles to read

 * http://news.softpedia.com/news/GNU-Screen-Tutorial-44274.shtml
 * http://www.kuro5hin.org/story/2004/3/9/16838/14935
 * http://www.kuro5hin.org/story/2004/3/9/16838/14935
 * http://www.math.mcgill.ca/services/linux_basics.php
 * http://gentoo-wiki.com/TIP_Using_screen
 * http://gameadmins.com/modules.php?name=Sections&op=viewarticle&artid=30
 * http://www.rackaid.com/resources/linux-tutorials/general-tutorials/linux-screen.cfm
 * http://www.fsid.cvut.cz/cz/U201/LINUX.HTML
 * http://www.linux.com/articles/113764


 * Another Screen Tutorial

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

https://www.kilobitspersecond.com/stuff/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

startup_message off
 * 1) skip the startup message

chdir
 * 1) go to home dir

autodetach on
 * 1) Automatically detach on hangup.

defscrollback 10000
 * 1) Change default scrollback value for new windows

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

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

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

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:
 * terminal - How can I launch a screen session with a command over ssh on a remote server from my desktop? - Server Fault - http://serverfault.com/questions/21806/how-can-i-launch-a-screen-session-with-a-command-over-ssh-on-a-remote-server-fro