Screen terminal multiplexer: Difference between revisions

From Alpine Linux
(delete Category:Networking)
(Restructuring a bit. Adding some notes and examples that indicates how screen can be used)
Line 1: Line 1:
'screen' is a good tool for 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 'screen' session). When you enter a 'screen' session you will not notice too much.<BR>
{{pkg|screen}} is a good tool for 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 {{pkg|screen}} session). When you enter a {{pkg|screen}} session you will not notice too much.<BR>
To know if you are inside a 'screen' session, use the notes in '[[#Get_help|get help]]' section mentioned below.
To know if you are inside a {{pkg|screen}} session, use the notes in '[[#Get_help|get help]]' section mentioned below.


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


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


== List existing sessions ==
== List existing sessions ==
When you have started some session(s) you can list them:
{{cmd|screen -list}}
{{cmd|screen -list}}
You might get a list that looks like this:
You might get a list that looks like this:
Line 20: Line 25:


== Attach to a existing session ==
== Attach to a existing session ==
Lets say you want to attach to a existing session (e.g. the above "11131.pts-3.mhlab01" 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}}
{{cmd|screen -x 11131}}
or
or
{{cmd|screen -x pts-3}}
{{cmd|screen -x pts-3}}
{{tip|If you see <code>Attaching from inside of screen?</code>, you are allready inside a screen session.}}
== Controlling a screen session ==
While inside a {{pkg|screen}} session, you can controll it using keyboard shortcuts. We will only describe some of those alternatives.
{{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>}}
=== Get help ===
One of the most useful commands is the one that gives you 'help'.<br>
While in your {{pkg|screen}} session, click:
{{cmd|^A ?}}
''(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 detatch from the session.<br>After some while re-connect to the session using <code>screen -x</code>.<br>Note that the "seq" value indikates that ping had continued running while you where detatched from the session.}}


== Close or kill a session ==
=== Close or kill a session ===
When you are inside a 'screen' session you can 'kill' it by using:
To 'kill' a session:
{{cmd|CTRL+A k}}
{{cmd|^A k}}
or you might just type:
Confirm by clicking {{key|y}} when prompted.
 
You can also 'kill' you session by entering:
{{cmd|exit}}
{{cmd|exit}}
== Get help ==
'screen' has various options/commands.<BR>
When your inside a 'screen' session you can get help and see what options you have by using:
{{cmd|CTRL+a ?}}
''(Do not press/hold 'CTRL' when entering ''''?'''')''


= Extra =
= Extra =
Line 41: Line 58:
In some cases you might want to force only console users into a screen session.<BR>
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>
Note that the this configuration will not force SSH-users into a screen.<BR>
Edit `/etc/profile` and add the following code to it:
Edit {{path|/etc/profile}} and add the following code to it:
<pre>
<pre>
if [ -n "$PS1" ] && [ -z "$STARTED_SCREEN" ] && [ -z "$SSH_TTY" ]; then
if [ -n "$PS1" ] && [ -z "$STARTED_SCREEN" ] && [ -z "$SSH_TTY" ]; then

Revision as of 10:05, 24 October 2012

screen is a good tool for 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 screen 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.

General

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 allready inside a screen session.

Controlling a screen session

While inside a screen session, you can controll 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 detatch from the session.
After some while re-connect to the session using screen -x.
Note that the "seq" value indikates that ping had continued running while you where detatched from the session.

Close or kill a session

To 'kill' a session:

^A k

Confirm by clicking y when prompted.

You can also 'kill' you session by entering:

exit

Extra

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 bash 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