How to use the screen command

Hello everyone, this is Liang Xu.

Many times, we need to perform some tasks that take a long time. If your network connection is suddenly disconnected at this time, then all the work you have done before may be lost, and the work you have done may have to be redone. This will waste a lot of our time and greatly affect our work. So is there a way to solve this problem?

The answer is yes, and that is to use screenthe command. screenThe command allows the user to use multiple terminal sessions in a window, and can disconnect or reconnect the disconnected session. The connection can be restored for each session, so even if the session is disconnected, the user does not have to worry about data loss, which just solves our problem.

Therefore, Liang Xu would like to share with you some of the functions and usage of this utility: Screen:

Install Screen

Now many Linux distributions usually have Screen pre-installed. You can check whether Screen is installed on the system by running the following command:

$ screen --version
Screen version 4.06.02 (GNU) 23-Oct-17 

If Screen is not installed on your system, you can execute the following command to install it:

$ sudo apt install screen

Start screen session

To start a Screen session, just enter the following command:

$ screen

Enter the screencommand, you'll see a full copyright information interface, you simply press the Enter or the spacebar you can skip this interface. After skipping, you can see the terminal interface. This terminal is no different from the original terminal. You can also browse folders and open files. It can also do things that can be done in the original terminal.

You can also start Screen session, use the -Sparameter to the session a name. For example, I want to start a call alvinof Screen sessions:

$ screen -S alvin

This can be used to distinguish other Screen sessions when you have started more than one Screen session.

After entering the Screen terminal, press the Ctrl a+?shortcut key, you can view all the commands that came Screen:

markdown

Detach and reconnect screen sessions

If you want to update the system, after you enter the update command, it takes a long time to complete the update. At this point, if you have not screencommand, once your network connection is lost, then the update will be interrupted, you must restart the update.

The separation function of Screen session can solve this problem. You can enter the screen terminal to execute the update command, and then enter the session separation shortcut key Ctrl a+d, you will see the following prompt message:

markdown

This means that you have successfully separated from the Screen session and returned to the original Shell session. At this time, the Screen session will continue to stay in the background to perform update tasks. And whether it is disconnected from the network or closing the original Shell session, it will not be affected.

Of course, if you do not have time to press the session separation shortcut key, the network will be disconnected, and the Screen session will not be closed. It will continue to stay in the background to perform update tasks.

If you want to reconnect to the detached Screen session to view the update process, just execute the following command:

$ screen -r

It should be noted that this command is only applicable to the situation that only one Screen session is started on the current terminal.

Nested Screen Session

Nested Screen session, as the name implies, is the Screen session in the Screen session, and it is one of the methods for users to use multiple terminals in one window.

Screen want to start a nested conversation is very simple, you just need to enter it again in the Screen terminal screencommand, or press the Ctrl a+cshortcut key, which will then start a conversation in your original Screen Screen session, while the original Screen session will be in the background Stay active.

For example, you run a topcommand to monitor the system's CPU usage:

$ top

markdown

And you do not want to turn off this topprocess, want to do other tasks at the same time, then you can use Ctrl a+cshortcut keys to start a new Screen session, so you can either keep the topcommand run in the background, but also to perform other tasks .

And if you want to switch back to execute topcommands Screen session, you can use shortcut keys Ctrl a+n(to switch to the next Screen session) or Ctrl a+p(Screen switches to the previous session) switch.

Detach and start a new Screen session

Separating from the Screen session and then starting a new Screen session can also enable users to use multiple terminals in one window.

For example, you start a Screen session, then enter df -hto view disk usage:

$ df -h

markdown

Then you can press Ctrl a+dseparate shortcut from the Screen session out, back to the original terminal. Then you can re-enter the screencommand to start a new session Screen to perform other tasks.

If you want to reconnect to run back to the df -hScreen session commands, you need to separate the first from the new Screen session and then try to enter screen -rcommand. At this time, because you started a number of parallel sessions Screen, but screencommand does not know what you want to reconnect to the session, so you will see the following message:

markdown

As you can see from the figure, each Screen session has its own ID (4352 and 4294) and its own name ( pts-0.lxlinux). You can see here that the names of the two sessions are both pts-0.lxlinux. That's because when I started the Screen session, I didn't specify a name, so they all use the default name as their name.

Want to reconnect to run the df -hScreen session commands, you simply screen -rcommand plus the ID of the session behind it, like this:

$ screen -r 4352

Of course, if the Screen when you start a session, a session you to specify a unique name, you can also specify by name which session to reconnect to the session, for example, to reconnect to the name for the alvinsession:

$ screen -r alvin

If you do not know the ID and name of the Screen session, you can also check it with the following command:

$ screen -ls

Close screen session

To close the Screen session is very simple, and closed the session as usual, you can use Ctrl dkeyboard shortcuts, you can also enter the exitcommand to shut down:

$ exit

markdown

Make good use of Screen

At work, we will always encounter tasks that need to be executed for a long time to complete. If we encounter network interruptions during the execution of these tasks, it will cause data loss and waste of time, which will seriously affect our jobs. Screen allows the process to continue running in the background after the session is disconnected, and supports the process to resume from the breakpoint, which solves this problem well.

And screencommand also allows us to use a window to perform multiple tasks simultaneously, which is very useful to our work. Therefore, in order to avoid loss of data and save time, we should be more use screencommand. Finally, if you also screenhave questions about orders, please leave a message to tell me chanting!

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/107990322