The screen command in Linux

screen is a free software developed by the GNU project for command line terminal switching. Users can connect to multiple local or remote command line sessions at the same time through the software and switch between them freely. GNU Screen can be seen as the command line interface version of the window manager. It provides a unified interface and corresponding functions for managing multiple sessions.

screen importance

The importance of screen is mainly reflected in that it can run on the server conveniently all the time, as long as the server is not down, and there is no power failure, it can be done without stopping. In fact, back-end engineers often encounter the need to execute scripts to process some data, but this script may need to be executed for half a day or a few days. Screen is the best choice. After a period of time, log in to the server to check the running status and see if the running is complete . Hang up and continue running without finishing.

Common screen parameters

The screen command has many parameters, let’s just look at the more commonly used ones

parameter Description
ls List all current answers
S Capital S, create a new session named xxx
r Connect process session by sequence number
d detach disconnects a session
D Same as the -d command, but will logout the user who was originally in screen

In each screen session, all commands start with ctrl+a

parameter Description
ctrl+a d Detach the session, throw the current screen session to the background for execution, and return to the state when it has not entered the screen. The screen continues to execute, even logout does not affect the background execution
ctrl+a k Forcibly close the current window
ctrl+a z Put the current session in the background for execution, and use the fg command to call it back

screen use

First simulate a script code that takes a long time to execute

test.php
<?php
sleep(10000);
echo 111;
?>

After the program sleeps for 10000 seconds, it outputs 1111.
Create a screen first, and then log out after executing it.

screen -S test
php test.php

Create a screen named test, and execute test.php after entering the screen. Then let the script execute in the screen, press Ctrl+ad to let the script execute in the background of the screen.

screen -S test
[detached from 3934.test]

exit Log out, wait for a while and then log in again to see if the script is still executing

ssh 
screen -ls
There are screens on:
	3934.test	(02/25/2020 10:27:06 PM)	(Detached)

The screen ID named test is 3934, connect to the screen and enter

screen -r 3934
php test.php

You can see that the script of test.php is still executing.

Execute ctrl+az, then the script is executed in the background, and then use fg to transfer to the foreground

[1]+  Stopped                 screen -r 3934
执行fg
php test.php

Look at screen -d and -D
to reopen a window, after ssh, execute

screen -d 3934
[3934.test detached.]
然后查看前一个窗口,看看是否断开了screen
screen -r 3934
[remote detached from 3934.test]

Look again, -D, in the first window

screen -r 3934
第二个窗口执行-D
screen -D 3934
[3934.test power detached.]
再看第一个窗口已经被强制退出登录了。
screen -r 3934
[remote power detached from 3934.test]
Connection to 127.0.0.1 closed.

As a developer, you only need to know three commands in normal development to meet more than 90% of the scope of use.

screen -ls
screen -S
screen -R
ctrl+a d

Guess you like

Origin blog.csdn.net/feifeixiang2835/article/details/103655421