GNU Screen is a terminal multiplexer (window manager). As the name suggests, Screen may be multiplexed between a plurality of physical terminal interactive shell, so we can perform different tasks at each terminal session. All sessions are completely independent of the Screen run the program. Therefore, even if the session closed unexpectedly or disconnected, running in the Screen session or process will continue to run. For example, when using SSH upgrade Ubuntu server, the screen
command will continue to run the upgrade process, just in case SSH session is terminated for any reason.
GNU Screen allows us to easily create multiple Screen session and switch between different sessions, copy text between sessions, at any time on or even out of the session and so on. It is one of the important commands for each Linux administrators should learn and use when necessary line tool. In this brief guide, we will see the screen
basic usage of commands and examples in Linux.
Install GNU Screen
GNU Screen in most Linux operating system's default repository are available.
To install GNU Screen on Arch Linux, run:
$ sudo pacman -S screen
复制代码
In Debian, Ubuntu, Linux Mint on:
$ sudo apt-get install screen
复制代码
On Fedora:
$ sudo dnf install screen
复制代码
On RHEL, CentOS:
$ sudo yum install screen
复制代码
On SUSE / openSUSE:
$ sudo zypper install screen
复制代码
Let's continue with the screen
example command.
A plurality of terminal session management command examples Screen
In the Screen default prefix shortcuts for all commands is Ctrl + a
. When you use Screen, you often need to use this shortcut. So, remember this keyboard shortcut.
Creating a new Screen Session
Let's create a new Screen session and connect to it. To do this, type the following command in the terminal:
screen
复制代码
Now, run any program or process in this session, even if you disconnect from this session, process or running program will continue to run.
Released from the Screen session
Out from the screen session, press Ctrl + a
and d
. You do not need to press the two key combination. First press Ctrl + a
and then press d
. From the conversation off, you will see something like the following output.
[detached from 29149.pts-0.sk]
复制代码
Here 29149
is the Screen ID, pts-0.sk
is the name of a screen session. You can use the Screen ID or session name corresponding to the company, out of the screen and terminates the session.
Create a named session
You can also create a Screen session with any custom name of your choice, instead of the default user name, as shown below.
screen -S ostechnix
复制代码
The above command will create a file named xxxxx.ostechnix
new Screen session and immediately connect to it. Out from the current session, press Ctrl + a
and press d
.
When you want to find out what processes are running on which sessions, naming the session would be useful. For example, when the system is disposed in the session LAMP, you can simply be named as follows.
screen -S lampstack
复制代码
Created out of session
Sometimes, you may want to create a conversation, but do not want to automatically connect to the session. In this case, run the following command to create a file named senthil
has been out of the session:
screen -S senthil -d -m
复制代码
It can be shortened to:
screen -dmS senthil
复制代码
The above command will create a named senthil
session, but will not connect to it.
Lists the screen session
To list all running sessions (or even on the off), run:
screen -ls
复制代码
Sample output:
There are screens on:
29700.senthil (Detached)
29415.ostechnix (Detached)
29149.pts-0.sk (Detached)
3 Sockets in /run/screens/S-sk.
复制代码
As you can see, I have three running sessions and all sessions are off.
Even on Screen Session
If you want to connect to the session, for example 29415.ostechnix
, simply run:
screen -r 29415.ostechnix
复制代码
or:
screen -r ostechnix
复制代码
Or Screen ID:
screen -r 29415
复制代码
To verify that we connect to the above-mentioned session, just list the open session and check.
screen -ls
复制代码
Sample output:
There are screens on:
29700.senthil (Detached)
29415.ostechnix (Attached)
29149.pts-0.sk (Detached)
3 Sockets in /run/screens/S-sk.
复制代码
As you can see in the above output, we now connect to the 29415.ostechnix
session. To exit the current session, press ctrl + a d
.
To create a nested conversation
When we run the screen
command, it creates a session for us. However, we can create nested session (a session within the session).
First, create a new session or even open session. Then I will create a file called nested
new session.
screen -S nested
复制代码
Now, in the session press Ctrl + a
and c
create another session. Just repeat this operation can create any number of nested Screen session. Each session will be assigned a number. Number from the 0
start.
You can press Ctrl + n
to move to the next session, and then press Ctrl + p
to move to the previous session.
The following are important session management nested list of keyboard shortcuts.
Ctrl + a "
- List all sessionsCtrl + a 0
- Switch to the session number 0Ctrl + a n
- switching to the next sessionCtrl + a p
- switching to the previous sessionCtrl + a S
- current region is divided horizontally into two regionsCtrl + a l
- The current region vertically divided into two regionsCtrl + a Q
- close all sessions except the current sessionCtrl + a X
- Close the current sessionCtrl + a \
- terminate all sessions and terminate ScreenCtrl + a ?
- Displays key bindings. To exit, press Enter
Lock Session
Screen has an option to lock the session. To do this, press Ctrl + a
and x
. Enter your password to lock Linux.
Screen used by sk <sk> on ubuntuserver.
Password:
复制代码
Recording session
You may want to record all the contents Screen session. To do this, simply press Ctrl + a
and H
can be.
Or, you can use the -L
parameters to start a new session to enable logging.
screen -L
复制代码
From now on, you do all the activities in the session will be recorded and stored in a $HOME
directory named screenlog.x
file. Here x
is a number.
You can use the cat
command or any text viewer to view the contents of the log file.
Screen recording session
Screen session termination
If you no longer need to session, just kill it. To kill called senthil
disengagement session:
screen -r senthil -X quit
复制代码
or:
screen -X -S senthil quit
复制代码
or:
screen -X -S 29415 quit
复制代码
If there is no open session, you will see the following output:
$ screen -ls
No Sockets found in /run/screens/S-sk.
复制代码
For more details, please refer to the man page:
$ man screen
复制代码
There is also a similar command line utility called Tmux, it performs the same work with GNU Screen. For more information, please refer to the following guidelines.
Resources
via: www.ostechnix.com/screen-comm…
Author: SK topics: lujun9972 Translator: wxy proofread: wxy
This article from the LCTT original compiler, Linux China is proud
Reproduced in: https: //juejin.im/post/5cffb475e51d45775f516a48