Screen terminal multiplexer: Difference between revisions

From Alpine Linux
(Use 'screen' on Alpine)
 
m (→‎Where to find more examples: fixed list formatting)
 
(14 intermediate revisions by 5 users not shown)
Line 1: Line 1:
This document is a short note on how to monitor what's happening on the console.
{{pkg|screen}} is a terminal multiplexer. It's a good tool for e.g. remote support. It can also be used to start a command you want to keep running after you close your console session (you can later on attach to your running session). When you enter a {{pkg|screen}} session you will not notice too much.<BR>
To know if you are inside a {{pkg|screen}} session, use the notes in '[[#Get_help|get help]]' section mentioned below.


= Install =
In order to use {{pkg|screen}} you will have to install it:
{{cmd|apk add screen}}


'''Install programs'''
= Usage =
  apk_add screen


== Start a new session ==
To enter a {{pkg|screen}} session you just enter:
{{cmd|screen}}


'''Make it autostart'''
== List existing sessions ==
When you have started some session(s) you can list them:
{{cmd|screen -list}}
You might get a list that looks like this:
<pre>
There are screens on:
        11151.pts-1.mhlab01    (Attached)
        11131.pts-3.mhlab01    (Attached)
2 Sockets in /var/run/screen/S-root.
</pre>
 
== Attach to a existing session ==
Lets say you want to attach to a existing session (e.g. the above <code>11131.pts-3.mhlab01</code> session).
{{cmd|screen -x 11131}}
or
{{cmd|screen -x pts-3}}
{{tip|If you see <code>Attaching from inside of screen?</code>, you are already inside a screen session.}}
 
== Controlling a screen session ==
While inside a {{pkg|screen}} session, you can control it using keyboard shortcuts. We will only describe some of those alternatives.


Lets edit '''/etc/profile''' and add the following code to it:
{{tip|To enter a keyboard shortcut that controls the current {{pkg|screen}} session, you should click {{key|A}} while holding down {{key|CTRL}}<br>In the below examples this procedure is described as <code>^A</code>}}
  if [ -n "$PS1" ] && [ -z "$STARTED_SCREEN" ] && [ -z "$SSH_TTY" ]; then
=== Get help ===
    STARTED_SCREEN=1 ; export STARTED_SCREEN
One of the most useful commands is the one that gives you 'help'.<br>
    screen -RR && exit 0
While in your {{pkg|screen}} session, click:
    echo "Screen failed! continuing with normal bash startup"
{{cmd|^A ?}}
  fi
''(Do not press/hold {{key|CTRL}} when clicking {{key|?}})''


=== Detach from a session ===
Sometimes it's useful to just detach from a session without killing it.<br>
{{cmd|^A d}}
{{tip|Try starting <code>ping 127.0.0.1</code> while inside a screen session and then detach from the session.<br>After some while re-connect to the session using <code>screen -x</code>.<br>Note that the "seq" value indicates that ping had continued running while you where detached from the session.}}


'''Not only the console...'''
=== Close or kill a session ===
To 'kill' a session:
{{cmd|^A k}}
Confirm by clicking {{key|y}} when prompted.


You can also 'kill' your session by entering:
{{cmd|exit}}
= Extra =
== Connect to serial console ==
{{pkg|screen}} is a good tool when you need to connect to a serial console ''(e.g. if you want to configure a switch using it's serial port)''. Connecting to a serial console could look like this:
{{cmd|screen /dev/ttyS0 9600}}
== Force console users into a screen session ==
In some cases you might want to force only console users into a screen session.<BR>
Note that the this configuration will not force SSH-users into a screen.<BR>
Edit {{path|/etc/profile}} and add the following code to it:
<pre>
if [ -n "$PS1" ] && [ -z "$STARTED_SCREEN" ] && [ -z "$SSH_TTY" ]; then
  STARTED_SCREEN=1 ; export STARTED_SCREEN
  screen -RR && exit 0
  echo "Screen failed! continuing with normal startup"
fi
</pre>
== Force console and SSH users into a screen session ==
The above example holds the if-statement:
The above example holds the if-statement:
  && [ -z "$SSH_TTY" ]
&& [ -z "$SSH_TTY" ]
This statement sees to that no ssh-sessions is forced into a screen.<BR>
Remove this part from above configuration to force SSH sessions into a screen session.<BR>
In case you want ssh-sessions to be forced into a screen, this option can be omitted.
{{Note|Console users will also be forced into a screen session when folowing these instructions}}
 
= Other terminal multiplexers =
A similar tool is {{pkg|tmux}} which is documented [[tmux|here]].
 
 
= Configuring .screenrc (example) =
<pre>
#hardstatus off
hardstatus alwayslastline '%{= kG}[ %{y}%H%? %1`%?%{g} ][%= %{= kw}%-w%{+b yk} %n*%t%?(%u)%? %{-}%+w %=%{g}][ %{y}%l %{g}][%{W}%c:%s %{g}]'
msgwait 1
vbell off
 
# Huge scrollback buffer
defscrollback 5000


# No welcome message
startup_message off


'''Check for available sessions'''
# Clear the screen after closing some programs
altscreen on


To check what available screen-sessions are available:
# Get rid of the vertical bars
  screen -list
rendition so =00
You might get a list that looks like this:
caption string "%{03} "
  There are screens on:
 
          11151.pts-1.mhlab01    (Attached)
# 256 colors
          11131.pts-3.mhlab01    (Attached)
term screen-256color
  2 Sockets in /var/run/screen/S-root.
terminfo rxvt-unicode 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
termcapinfo xterm* ti@:te@
attrcolor b ".I"
 
# UTF-8
#defutf8 on
#utf8 on
 
# Default Windows


# Switch windows with F3 (prev) and F4 (next)
bindkey "^[OR" prev
bindkey "^[OS" next


'''Attach to a session'''
# Get rid of silly xoff stuff
bind s split
bind c screen 1
bind ^c screen 1
bind 0 select 10                                                           
screen 1


Lets say you want to attach to a existing session (e.g. the above "11131.pts-3.mhlab01" session).
# remove some stupid / dangerous key bindings
  screen -x pts-3
bind k
bind ^k
bind .
bind ^\
bind \\
bind ^h
bind h  hardcopy
bind 'K' kill
bind '}' history


bind L screen -t dmesg 10 watch "dmesg | tail -n $((LINES-42))"
bind T screen -t htop 11 htop
bind A screen -t atop 12 atop
bind N screen -t nethogs 15 nethogs
bind V screen -t vnstat 16 vnstat
bind S screen -t ss 17 ss -s
</pre>


'''Help'''
== Where to find more examples ==
* Archlinux forums (https://bbs.archlinux.org/viewtopic.php?id=55618)
* GithubGist (https://gist.github.com/search?utf8=%E2%9C%93&q=screenrc)
* Github (https://github.com/search?utf8=%E2%9C%93&q=screenrc)


'screen' has various options/commands.<BR>
[[Category:Shell]]
When your inside a 'screen'-session you can:
  CTRL+a ?
(Don't hold CTRL when entering ''''?'''')

Latest revision as of 05:18, 9 November 2024

screen is a terminal multiplexer. It's a good tool for e.g. remote support. It can also be used to start a command you want to keep running after you close your console session (you can later on attach to your running session). When you enter a screen session you will not notice too much.
To know if you are inside a screen session, use the notes in 'get help' section mentioned below.

Install

In order to use screen you will have to install it:

apk add screen

Usage

Start a new session

To enter a screen session you just enter:

screen

List existing sessions

When you have started some session(s) you can list them:

screen -list

You might get a list that looks like this:

There are screens on:
        11151.pts-1.mhlab01     (Attached)
        11131.pts-3.mhlab01     (Attached)
2 Sockets in /var/run/screen/S-root.

Attach to a existing session

Lets say you want to attach to a existing session (e.g. the above 11131.pts-3.mhlab01 session).

screen -x 11131

or

screen -x pts-3

Tip: If you see Attaching from inside of screen?, you are already inside a screen session.

Controlling a screen session

While inside a screen session, you can control it using keyboard shortcuts. We will only describe some of those alternatives.

Tip: To enter a keyboard shortcut that controls the current screen session, you should click A while holding down CTRL
In the below examples this procedure is described as ^A

Get help

One of the most useful commands is the one that gives you 'help'.
While in your screen session, click:

^A ?

(Do not press/hold CTRL when clicking ?)

Detach from a session

Sometimes it's useful to just detach from a session without killing it.

^A d

Tip: Try starting ping 127.0.0.1 while inside a screen session and then detach from the session.
After some while re-connect to the session using screen -x.
Note that the "seq" value indicates that ping had continued running while you where detached from the session.

Close or kill a session

To 'kill' a session:

^A k

Confirm by clicking y when prompted.

You can also 'kill' your session by entering:

exit

Extra

Connect to serial console

screen is a good tool when you need to connect to a serial console (e.g. if you want to configure a switch using it's serial port). Connecting to a serial console could look like this:

screen /dev/ttyS0 9600

Force console users into a screen session

In some cases you might want to force only console users into a screen session.
Note that the this configuration will not force SSH-users into a screen.
Edit /etc/profile and add the following code to it:

if [ -n "$PS1" ] && [ -z "$STARTED_SCREEN" ] && [ -z "$SSH_TTY" ]; then
  STARTED_SCREEN=1 ; export STARTED_SCREEN
  screen -RR && exit 0
  echo "Screen failed! continuing with normal startup"
fi

Force console and SSH users into a screen session

The above example holds the if-statement:

&& [ -z "$SSH_TTY" ]

Remove this part from above configuration to force SSH sessions into a screen session.

Note: Console users will also be forced into a screen session when folowing these instructions

Other terminal multiplexers

A similar tool is tmux which is documented here.


Configuring .screenrc (example)

#hardstatus off
hardstatus alwayslastline '%{= kG}[ %{y}%H%? %1`%?%{g} ][%= %{= kw}%-w%{+b yk} %n*%t%?(%u)%? %{-}%+w %=%{g}][ %{y}%l %{g}][%{W}%c:%s %{g}]'
msgwait 1
vbell off

# Huge scrollback buffer
defscrollback 5000

# No welcome message
startup_message off

# Clear the screen after closing some programs
altscreen on

# Get rid of the vertical bars
rendition so =00
caption string "%{03} "

# 256 colors
term screen-256color
terminfo rxvt-unicode 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
termcapinfo xterm* ti@:te@
attrcolor b ".I" 

# UTF-8
#defutf8 on
#utf8 on

# Default Windows

# Switch windows with F3 (prev) and F4 (next)
bindkey "^[OR" prev
bindkey "^[OS" next

# Get rid of silly xoff stuff
bind s split
bind c screen 1
bind ^c screen 1
bind 0 select 10                                                            
screen 1

# remove some stupid / dangerous key bindings
bind k
bind ^k
bind .
bind ^\
bind \\
bind ^h
bind h  hardcopy
bind 'K' kill
bind '}' history

bind L screen -t dmesg 10 watch "dmesg | tail -n $((LINES-42))"
bind T screen -t htop 11 htop
bind A screen -t atop 12 atop
bind N screen -t nethogs 15 nethogs
bind V screen -t vnstat 16 vnstat
bind S screen -t ss 17 ss -s

Where to find more examples