Friday, July 26, 2013

Linux - GNU Screen instructions

GNU Screen is a command line application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command line interface, and for separating programs from the shell that started the program." Remember, Screen was designed in 1987 but is still quite useful.

For a full HTML manual use the following link (http://www.gnu.org/software/screen/manual/screen.html)

There are a few real world usage cases that make Screen very valuable.  You may want to start a command line based application or process then disconnect from it without closing or interrupting the process and come back to it later. Disconnection could be due to closing of an SSH session or because you needed to start the process but someone else needs to finish it... more on this to follow.

There are a few "Quick Reference Guides" out there but for most cases the following* (http://aperiodic.net/screen/quick_reference) reference guide will do the trick (*edited here):

Getting in

start a new screen session with a session name <name> screen -S <name>
list running sessions/screens screen -ls
reattach to a running session screen -r
… to session with name screen -r <name>
the “ultimate attach” screen -dRR (Attaches to a screen session. If the session is attached elsewhere, detaches that other display. If no session exists, creates one. If multiple sessions exist, uses the first one.)
start in multi-user mode screen -x if only one screen session is started this will connect to it in multiuser mode. If multiple sessions you will need to define the session by name or number.
start connecting to tty/serial connection screen <device> <baud_rate>
Mac ex: using a USB-serial adapter:screen /dev/cu.usbserial 115200
Linux ex: using the serial port:screen /dev/ttyS0 115200
Additional windows can be made as normal, however, to close this window (the serial window) use C-a k

Escape key

All screen commands are prefixed by an escape key, by default C-a (that's Control+a, sometimes written ^a). To send a literalC-a to the programs in screen, use C-a a.

Getting out

detach C-a d
detach and logout (quick exit) C-a D D
exit screen “C-a : quit” or exit all of the programs in screen.
force-exit screen C-a C-\ (not recommended)

Help

See help C-a ? (lists keybindings)

The man page is the complete reference, but it's very long.

Window Management

create new window C-a c
change to last-visited active window C-a C-a (commonly used to flip-flop between two windows)
change to window by number C-a <number> (only for windows 0 to 9)
change to window by number or name C-a ' <number or title>
change to next window in list C-a n or C-a <space>
change to previous window in list C-a p or C-a <backspace>
see window list C-a " (allows you to select a window to change to)
show window bar C-a w (if you don't have window bar)
close current window Close all applications in the current window (including shell)
kill current window C-a k (not recommended) used if connecting to a tty serial port
kill all windows C-a \ (not recommended)
rename current window C-a A

Split screen

split display horizontally C-a S
split display vertically C-a | or C-a V (for the vanilla vertical screen patch)
jump to next display region C-a tab
remove current region C-a X
remove all regions but the current one C-a Q

Scripting

send a command to a named session screen -S <name> -X <command>
create a new window and run ping example.com screen -S <name> -X screen ping example.com
stuff characters into the input buffer
using bash to expand a newline character
(from here)
screen -S <name> [-p <page>] -X stuff $'quit\r'
a full example
# run bash within screen
screen -AmdS bash_shell bash
# run top within that bash session
screen -S bash_shell -p 0 -X stuff $'top\r'
 
# ... some time later
 
# stuff 'q' to tell top to quit
screen -S bash_shell -X stuff 'q'
# stuff 'exit\n' to exit bash session
screen -S bash_shell -X stuff $'exit\r'

Misc

redraw window C-a C-l
enter copy mode C-a [ or C-a <esc> (also used for viewing scrollback buffer)
paste C-a ]
monitor window for activity C-a M
monitor window for silence C-a _
enter digraph (for producing non-ASCII characters) C-a C-v
lock (password protect) display C-a x
enter screen command C-a :

There are more options to make screen more visually functional which can be found at the following link (http://www.debian-administration.org/articles/560). I have also incorporated some of the options into the following screen configuration file.

More can be configured through a screen config file.  In the user's home directory make a file .screenrc that contains the options

#CODE: example .screenrc file
defscrollback 5000
altscreen on
shell -/bin/bash
hardstatus alwayslastline "%{= g} %{= w}%-Lw%{=r}%n%f* %t%{-}%+LW"
  1. defscrollback 5000 = 5000 lines of screen scroll back memory
  2. altscreen on = allows screen to act like the standard virtual terminal window and clear text at the exit of a program like vim, less, or more
  3. shell -/bin/bash = allows screen to keep the standard shell prompt prefix/title instead of "bash-3.2$"
  4. hardstatus alwayslastline ... = always keep the last line as a title list of current windows and use a "-" to show the last window used and highlight and place an "*" next to the current selected window.

For a more complete walkthrough visit (https://docs.loni.org/wiki/The_Door_to_Screen/Table_of_Contents)

No comments:

Post a Comment