Linux background terminal

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 screento 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+Ddetach 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+Dleave 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.
    insert image description here
    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, -Sthe name will be randomly assigned

  • screen -r <终端ID>:resume, restore the screen terminal, ctrl+A+Dthe 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 to ctrl+A+Doffline terminals, you can also use -dparameters to manually offline terminals

  • screen -S <终端ID> -X quit: Completely end a terminal and destroy all the programs it runs

Guess you like

Origin blog.csdn.net/qq_40243750/article/details/130727310