How to install and use screen, a remote session management tool for CentOS 7 system?


You may encounter such a problem: use SSH or telent to remotely log in to the Linux server and run some tasks that take a long time to complete, such as system backup, FTP transmission and so on. Usually, we open a remote terminal window for each of these tasks, because they take too long to execute. You must wait for them to be executed. During this period, you can’t close the window or disconnect. If you suddenly disconnect due to network abnormality or other factors, the remote command will also stop. You can only run it after reconnection, and everything will be abandoned halfway. Here I will teach you how to use the screen command to avoid the above problems.

Screen is a full screen window manager that can multiplex a physical terminal between multiple processes. Users can create multiple screen sub sessions in one screen session. In each screen session (or sub session), it is like operating a real telnet / SSH connection window.

Install screen

1. Use putty or similar SSH tools to log in to CentOS 7 server; directly enter screen to see the prompt “bash: screen: command not found…”; whether screen is installed in the car inspection system;

2. If your CentOS system does not have its own screen, the installation method is as follows:

yum install screen

After the installation is successful, enter screen to switch to another window. You can switch back to the original command line interface through Ctrl + A + D;

Create screen session

scene: you need to download and install the LNMP one click installation package in CentOS 7;

1. First execute the following command screen – s shapolang. The above command means: create a new screen session named shapolang.

2. Start installing LNMP,

a. Execute WGet — no check certificate lnmp/soft/lnmp1.2- full.tar.gz Download the installation package.

b. Execution: tar – xvf lnmp1.2- full.tar.gz Decompress.

c. Execute: CD lnmp1.2-full / enter the lnmp1.2 directory.

d. Execution:/ Install.

If the network is offline, you can connect again, and then execute “screen – R shapolang” to see your shapolang installation process.

Leave the screen session temporarily

(but keep the LNMP one click installation package command running in screen)

In the SSH window, press the shortcut key: Ctrl + a D (hold down Ctrl, press a in turn, and then press D) to exit the screen session named shapolang.

When you leave the screen session temporarily, the screen session will not stop because you leave. The running process (such as compiling the LNMP one click installation package) will continue to run. The advantage of screen is that you can perform multiple tasks in the same SSH window, such as opening a new screen session to install gitlab. You can also temporarily close the SSH window and do other things.

Reread screen session

Execute the above command in SSH to read. If you forget the name of the previously created screen session or there are multiple sessions with the same name when creating, you can query the running screen session list information through the following command.

screen -ls

1. Read through the screen name (as previously created shapolang).

screen -r shapolang

If there are multiple names with the same name, there will be session ID + session name before the session name, such as 4054. Shapolang;

You can get the specified session through screen – R 4054.shapolang or screen – R 4054 (the latter is recommended, the first is more complex)

Close screen session

If the current screen session is no longer needed, you can execute exit in the current screen session, and prompt [screen is terminating] to indicate that the screen session has been successfully closed (it will not be kept in memory). If necessary, please rebuild.

Shortcut key for screen

As mentioned before, Ctrl + a D can temporarily leave the current screen session, and screen also provides other shortcut keys.

CTRL + a C: creates a child session in the current screen session

CTRL + a p: Previous sub session

CTRL + a n: next sub session

matters needing attention:

The operation is very simple, but it’s better not to create too many sessions with the same name. After all, the reply ID is not intuitive.

Recommended Today

Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]