Basic use of screen command in Linux system

Time:2020-10-30

As a Linux server administrator, they often use SSH to log in to the remote Linux machine to do some time-consuming operations.

You may have encountered telnet or SSH Remote Login to Linux and run some programs. If these programs need to run for a long time (several hours), and there is network failure or client failure during the program running, then the link between the client and the remote server will be terminated, and the command that the remote server does not end normally will be forced to terminate.

Another example is that after you SSH to the host, you start batch SCP commands. If the SSH thread is disconnected, the SCP process will be interrupted. Some time-consuming jobs are running on the remote server, but the work is not finished and will be off duty soon. If you exit, the operation will be interrupted. How can I do it?

We can use the screen command to solve this problem. In the case of disconnection of SSH, continue to execute the program on the server.

What is the screen command?

Screen, known as a full screen window manager, can easily get the effect of multiple virtual terminals on one physical terminal.

Screen function description:

Simply put, screen is a window manager that multiplexes a physical terminal between multiple processes, which means that you can use a single terminal window to run multi terminal applications. There is the concept of session in screen. Users can create multiple screen windows in a screen session. In each screen window, it is like operating a real telnet / SSH connection window.

Screen command syntax:

Screen [- amrvx – LS – rope] [- d < job name >] [- H < number of rows >] [- R < job name >] [- S] [- s < job name >]

Screen command parameters:
-A – [r| R] adjusts all windows to the size of the current terminal.
-C filename replaces screen’s configuration file ‘. Screenrc’ with the specified file name
-d [ pid.tty.host ]Disconnect the screen process (when using this command, the status of screen must be attached, that is, there is a user connected to the screen). The general process name is pid.tty.host In this form (you can see the state with the screen – list command).
-D [ pid.tty.host ]It has the same function as the – D command. The difference is that if the execution is successful, the user in the screen will be kicked off and he will be logged out.
-H < rows > specifies the number of buffer rows for the window.

-Ls or – list displays all screen jobs currently.
-M. force the creation of a new screen job even if the screen job is currently in the job.
-P number or name to preselect a window.
-r [ pid.tty.host ]Recover the offline screen process. If there are multiple disconnected processes, you need to specify[ pid.tty.host ]
-R first attempts to resume the offline job. If no offline job is found, a new screen job is created.
-S shell specifies the shell to execute when creating a new window.
-S < job name > specifies the name of the screen job. (used in place of[ pid.tty.host ]Can simplify the operation
-V display version information.
-Wipe checks all current screen jobs and deletes screen jobs that are no longer available.
-X. restore the screen job that was offline before.

General usage of the screen command:

Screen – D – R: connect a screen process. If the process is attached, it will kick off the remote user before connecting.

Screen – D – R: connect a screen process. If the process is attached, kick off the remote user and let him log out before connecting

Screen – LS or – List: displays the existing screen process, commonly used commands

Screen – M: if in a screen process, use the shortcut key crtl + a C or directly type screen to create a new window, and screen – M can create a new screen process.

Screen – DM: create a new screen with detached mode by default, that is, it will not be connected after it is created.

Screen – P number or name: select a window in advance.

Screen simple steps to implement background running program:

1> To do something, use the command to create a screen:

Copy code

The code is as follows:

[linux@user~]$ screen -S test1

2> Then you can operate in it. If you want to leave before the task is completed, use the command to keep screen:

Copy code

The code is as follows:

[ [email protected] ~]$Ctrl + A + D ා press Ctrl + A, then d to keep the screen
[detached] ා this prompt will be displayed, indicating that the screen has been reserved

If you are finished, you can enter:

Copy code

The code is as follows:

[ [email protected] ~]$exit ා this indicates successful exit
[screen is terminating]

3> If you kept screen the last time, you can use the command to view:

Copy code

The code is as follows:

[[email protected]~]$ screen -ls
There is a screen on:
9649.test1 (Detached)

To restore screen, use the command:

Copy code

The code is as follows:

[[email protected]~]$ screen -r test1 (or 9649)

Shortcut keys used in the screen command

CTRL + a C: create window

CTRL + a W: Window List

CTRL + a n: next window

CTRL + a p: previous window

CTRL + a 0-9: switch between window 0 and window 9

CTRL + a K (upper case): close the current window and switch to the next window (when you exit the last window, the terminal will automatically terminate and return to the original shell state)

Exit: close the current window and switch to the next window (when you exit the last window, the terminal will automatically terminate and return to the original shell state)

CTRL + a D: exit the current terminal and return to the shell command state before loading screen

Multi window
Screen, like many window managers, can support multiple windows. This feature is useful when working on multiple tasks without opening a new session. As a system administrator, I often have four or five SSH sessions at the same time. Under each shell, I might have to deal with two or three tasks. Without screen, you need 15 SSH sessions, 15 logins, 15 windows, and so on. With screen, each system is assigned to a separate session, and I use screen to manage different jobs on the system.
To open a new window, just use “ctrl-a” “C”. The new window created displays a default command prompt. For example, I can run the top command and open a new window to do other work. Top keeps running there! You can experiment with it by starting screen and running top. Note: to save space, I truncated multiple screens
Start top

Copy code

The code is as follows:

Mem: 506028K av, 500596K used, 5432K free,
0K shrd, 11752K buff
Swap: 1020116K av, 53320K used, 966796K free
393660K cached</p>
<p> PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
6538 root 25 0 1892 1892 596 R 49.1 0.3
6614 root 16 0 1544 1544 668 S 28.3 0.3
7198 admin 15 0 1108 1104 828 R 5.6 0.2

You can now open a new window by “ctrl-a” “C”

Copy code

The code is as follows:

[[email protected] admin]$
To get back to top, use “Ctrl-A “n”
Mem: 506028K av, 500588K used, 5440K free,
0K shrd, 11960K buff
Swap: 1020116K av, 53320K used, 966796K free
392220K cached</p>
<p> PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
6538 root 25 0 1892 1892 596 R 48.3 0.3
6614 root 15 0 1544 1544 668 S 30.7 0.3

You can create multiple windows and then switch to the next window with “ctrl-a” “n”, or use “ctrl-a” “P” to return to the previous window. When you work in other windows, every program in other windows will keep running.
Exit screen
There are two ways to exit screen. First, you can terminate a window by “ctrl-a”, “K” or “exit” just as you log out of a shell. In this way, the current window will be closed. If you open multiple windows, you will go directly to one of the others. If it is the only window, you will exit the screen.
Another way to exit screen is to detach the window. This simply closes the window, but the process is still running. If you have a process that you want to execute for a long time and you need to close the SSH program, you can use “ctrl-a” and “d” to separate the window. This will bring you back into the shell. All the screen windows stay there and you can take them back later. (translator’s note: it’s very similar to our actual minimization of windows and running programs in the background.)
Take over session
Suppose you are using screen to compile a program for a long time, and suddenly your connection is disconnected. Don’t worry, screen will save your compilation progress. After logging in to your operating system again, use the screen list tool to see which sessions are running:

Copy code

The code is as follows:

[[email protected] root]# screen -ls
There are screens on:
31619.ttyp2.gigan (Detached)
4731.ttyp2.gigan (Detached)
2 Sockets in /tmp/screens/S-root.

Here, I have two different screen sessions. To take over one of them again, use the command in the recovery window:

Copy code

The code is as follows:

[[email protected] root]#screen -r 31619.ttyp2.gigan

Just use the – R option followed by the name of the session, and now you can go back to the screen. Happily, you can take over again anywhere. Whether in the office or on other clients, you can use screen to start a job and exit.

Multi window
Screen, like many window managers, can support multiple windows. This feature is useful when working on multiple tasks without opening a new session. As a system administrator, I often have four or five SSH sessions at the same time. Under each shell, I might have to deal with two or three tasks. Without screen, you need 15 SSH sessions, 15 logins, 15 windows, and so on. With screen, each system is assigned to a separate session, and I use screen to manage different jobs on the system.
To open a new window, just use “ctrl-a” “C”. The new window created displays a default command prompt. For example, I can run the top command and open a new window to do other work. Top keeps running there! You can experiment with it by starting screen and running top. Note: to save space, I truncated multiple screens

Start top

Copy code

The code is as follows:

Mem: 506028K av, 500596K used, 5432K free,
0K shrd, 11752K buff
Swap: 1020116K av, 53320K used, 966796K free
393660K cached</p>
<p> PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
6538 root 25 0 1892 1892 596 R 49.1 0.3
6614 root 16 0 1544 1544 668 S 28.3 0.3
7198 admin 15 0 1108 1104 828 R 5.6 0.2

You can now open a new window by “ctrl-a” “C”

Copy code

The code is as follows:

[[email protected] admin]$
To get back to top, use “Ctrl-A “n”
Mem: 506028K av, 500588K used, 5440K free,
0K shrd, 11960K buff
Swap: 1020116K av, 53320K used, 966796K free
392220K cached</p>
<p> PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
6538 root 25 0 1892 1892 596 R 48.3 0.3
6614 root 15 0 1544 1544 668 S 30.7 0.3

You can create multiple windows and then switch to the next window with “ctrl-a” “n”, or use “ctrl-a” “P” to return to the previous window. When you work in other windows, every program in other windows will keep running.

Exit screen
There are two ways to exit screen. First, you can terminate a window by “ctrl-a”, “K” or “exit” just as you log out of a shell. In this way, the current window will be closed. If you open multiple windows, you will go directly to one of the others. If it is the only window, you will exit the screen.
Another way to exit screen is to detach the window. This simply closes the window, but the process is still running. If you have a process that you want to execute for a long time and you need to close the SSH program, you can use “ctrl-a” and “d” to separate the window. This will bring you back into the shell. All the screen windows stay there and you can take them back later. (translator’s note: it’s very similar to our actual minimization of windows and running programs in the background.)
Take over session
Suppose you are using screen to compile a program for a long time, and suddenly your connection is disconnected. Don’t worry, screen will save your compilation progress. After logging in to your operating system again, use the screen list tool to see which sessions are running:

Copy code

The code is as follows:

[[email protected] root]# screen -ls
There are screens on:
31619.ttyp2.gigan (Detached)
4731.ttyp2.gigan (Detached)
2 Sockets in /tmp/screens/S-root.

Here, I have two different screen sessions. To take over one of them again, use the command in the recovery window:

Copy code

The code is as follows:

[[email protected] root]#screen -r 31619.ttyp2.gigan

Just use the – R option followed by the name of the session, and now you can go back to the screen. Happily, you can take over again anywhere. Whether in the office or on other clients, you can use screen to start a job and exit.