Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

We often need to remote login to Linux server, run some take a long time to finish tasks or scripts, usually we are for each of these tasks alone open a terminal window, because they perform the execution time is too long, in the process of task execution and can’t close the terminal window or disconnected, Otherwise the mission will be killed, and it will fall short. In this case, using the Linux screen command can effectively avoid this problem. Let’s take a look at the use of this tool.

Screen is a command line terminal switching tool that allows users to simultaneously connect to multiple local or remote command line sessions and switch between them freely. In Screen, all sessions run independently, with their own numbers, inputs, outputs, and window caches. Users can switch between different Windows by shortcut keys, and can freely redirect the input and output of each window.

Install the screen

Screen is preinstalled on some popular Linux distributions, and you can use the screen -v command to check if it is installed on the server.

If screen is not installed, you can install it using the package manager provided by the system.

CentOS:

yum -y install screen
Copy the code

Ubuntu:

apt-get -y install screen
Copy the code

use

Typically, screen creates terminals that work in two modes.

  • Attached: Indicates that Screen is currently in use as the master terminal and is active.
  • Detached: Indicates that the current screen is being used in the background, in a non-fired state.

grammar

Screen [- AmRvx - ls - wipe] [-d < job name >] [-h < number > lines] [-r name > < homework] [-s < shell >] [-s < job name >]Copy the code

Parameter Description:

  • -A Resize all Windows to the current terminal size.
  • -d < Job name > takes the specified Job Screen offline.
  • -h < row number > Specifies the number of buffer lines for the window.
  • -m Forces the creation of a new Screen job even if the screen job is already in the job.
  • -r < Job name > Restores offline Job Screen.
  • -r Attempts to recover the offline job first. If no offline job is found, create a new Screen job.
  • -s Specifies the shell to execute when a new window is created.
  • -s < Job name > Specifies the name of the Screen job.
  • -v Displays the version information.
  • -x Restores the screen job that was offline before.
  • -ls or –list displays all current screen jobs.
  • – Wipe all current Screen jobs and delete screen jobs that are no longer available.

Start screen Terminal

#Create a screen terminal named name using -s
screen -S name
Copy the code

or

#Create a screen terminal named name using -r
screen -R name
Copy the code

After that, a blank Terminal will be created, which is the new Screen Terminal. We can run the program or script we want to run in this new Terminal.

Leave the Screen terminal

To leave the current Screen terminal and return to the main terminal, press Ctrl + A and D. After leaving, the Screen session will remain active and can be reconnected at any time later.

Reconnect to the Screen terminal

screen -r
Copy the code

If you have multiple Screen sessions, you can list them with the ls argument.

screen -ls There are screens on: (Detached, 2021年09月30, 10 分 19分34秒) (Detached, 2021年09月30, 10 分 19分34秒 (Detached) 3 Sockets in /run/screen/ s-pSH (Detached) 3 Sockets in /run/screen/ s-PSHCopy the code

As shown above, there are three active Screen sessions. If you want to reconnect to the “session2” session, you can do:

screen -r 45195
Copy the code

or

screen -r -S session2
Copy the code

Abort screen terminal

You can press Ctrl+ D, or use the exit command from the command line.

To see all the features of the Screen command, you can use the man screen command to see screen’s man manual.

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!