1. Background terminal
When we connect a terminal and execute a program, the program is also terminated when the terminal is closed. For example, if you want to execute a web server in the terminal and want to keep running in the background, you can use the tool screen
2. screen tool
The screen tool does not come with it, so it needs sudo apt update && sudo apt install screen
to be installed.
In the terminal, you can open a new terminal to run the web service: screen -S name
, and then we enter a new terminal. After running the server, use the shortcut key to ctrl+A+D
detach the terminal and return to the original terminal. Note that leaving here is more like minimizing the terminal, not ending the terminal. The program we are running in the screen does not end. When we return to the original terminal ( ctrl+A+D
leave the returned terminal), we can directly close the terminal, and our screen terminal is still running.
Summary of common commands
-
screen -S name
: Create a screen terminal named name -
screen -ls
: Displays the screen terminal that has been created.
The number in front is the unique ID of each screen terminal, and the letter after it is the name obtained when creating it. If it is not specified at the time of creation,-S
the name will be randomly assigned -
screen -r <终端ID>
:resume, restore the screen terminal,ctrl+A+D
the inverse operation -
screen -R <终端ID>
: First try to restore the screen terminal, if the terminal does not exist, create a new screen terminal directly -
screen -d <终端ID>
: detach, in addition toctrl+A+D
offline terminals, you can also use-d
parameters to manually offline terminals -
screen -S <终端ID> -X quit
: Completely end a terminal and destroy all the programs it runs