Linux - Basic use of Screen

install screen

First check if it is installed

screen --version
screen -v

If not installed, CentOS enter the following command to install

yum install screen

Ubuntu and Debian enter the following command to install

apt install screen

screen function

There are generally three functions of screen:

(1) Session recovery: As long as the Screen itself is not terminated, the sessions running inside it can be recovered. This is especially useful for users who are logged in remotely - even if the network connection is lost, the user will not lose control of the command line session they have opened. Just log in to the host again and execute screen -r to resume the session. Similarly, when leaving temporarily, you can also execute the detach command to suspend the Screen (switch to the background) while ensuring the normal operation of the programs inside. This is very similar to VNC under the graphical interface.
(2) Multi-window: In the Screen environment, all sessions run independently and have their own number, input, output and window cache. Users can switch between different windows through shortcut keys, and can freely redirect the input and output of each window.
(3) Session sharing: Screen allows one or more users to log in to a session multiple times from different terminals, and share all the features of the session (for example, you can see exactly the same output). It also provides a window access mechanism, which can password-protect the window.

state of the screen

Normally, the virtual terminal created by screen has two working modes:

  • Attached: Indicates that the current screen is being used as the main terminal and is active.
  • Detached: Indicates that the current screen is being used in the background and is in an inactive state.

screen query help document

screen -help

You can see the following options
insert image description here

New screen terminal

When you start a new screen session, it creates a single window with a shell.
You can have multiple windows in a screen session.

screen
screen -S Hello
screen -R Hello

The difference between the three creation methods:

  • Use -R to create: If there is only one screen with the same name created before, it will directly enter the previously created screen. (* recommended use )
  • Use -S to create: the screen created before will not be checked (that is, the screen with the same name will be created).
  • Direct screen creation: Named after the Linux hostname, such a name is too messy.

View existing screen terminals

screen -ls

Return from virtual terminal to main terminal

Press Ctrl + a in the virtual terminal, then press d to keep the screen in the background and return to the main terminal.

Enter the virtual terminal

Quickly return to the last virtual terminal you were in

screen -rxx

To return to the specified terminal, use -R or -r:

screen -r [pid/name]

Among them, pid/name: is the PID or Name of the virtual terminal. View it through screen -ls, and you can see that 32307 is the PID, tool is the Name, and Detached is the status.
insert image description here
Enter the following command in the main terminal to enter the specified virtual terminal:

screen -r 32307
# 或(在没有重名虚拟终端情况下)
screen -r tool

clear terminal

Sometimes, our process has been "guarded" and the virtual terminal is no longer needed, that is, resources need to be released. How to do it? The recommended
method is: enter the corresponding virtual terminal, and then enter: exit, it will return to the main terminal, check the terminal and find that the terminal is cleared.

If you run the program on the screen, make sure it has stopped running, or you can use the command to release it in the main terminal:

# 使用-R/-r/-S均可
screen -R [pid/Name] -X quit

bind key

In the virtual terminal, enter Ctrl+a to wait for the preset binding key to be accepted. At this time, you can enter some corresponding commands to operate the virtual terminal, such as:

  • d: save the session, run in the background and change to a virtual terminal
  • k: close the dialog, equivalent to input: exit
  • c: Creates a new window and assigns to it the first available number starting from the range 0...9
  • ": list all windows
  • 0: switch to window 0 (enter other numbers to switch to the corresponding window)
  • ?: Show all bound keyboards

Reference:
Linux Terminal Command Artifact – Detailed Explanation of Screen Command.

Guess you like

Origin blog.csdn.net/DreamingBetter/article/details/124037647