Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of screen command in Linux

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Next, please follow the editor to study!

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

The answer is yes, which is to use the screen command. The screen command allows users to use multiple terminal sessions within a window, either to disconnect or to reconnect disconnected sessions. Each session can restore the connection, so that even if the session is disconnected, the user does not have to worry about data loss, which solves our problem.

Therefore, Liangxu would like to share with you some of the features and usage of Screen:

Install Screen

Nowadays, many Linux distributions usually come pre-installed with Screen. You can see if 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 a Screen session

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

$screen

After typing the screen command, you will see an interface full of copyright information, which you can skip by simply pressing enter or the space bar. After skipping, you can see the terminal interface. This terminal is no different from the original terminal. You can also browse folders, open files, and do what you can do in the original terminal.

You can also use the-S parameter to name the session when you start the Screen session. For example, I want to start a Screen session called alvin:

$screen-S alvin

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

After entering the Screen terminal, press Ctrl averse? Keyboard shortcuts to view all the commands that come with Screen:

Markdown

Detach and reconnect Screen session

If you want to update the system, it will take a long time to update after you enter the update command. At this point, if you do not use the screen command, once your network connection is disconnected, the update will be interrupted and you will have to start the update again.

The separation of Screen sessions can solve this problem. You can enter the Screen terminal to execute the update command, and then type the session separation shortcut Ctrl aquid, and you will see the following prompt:

Markdown

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

Of course, if you don't have time to press the session detach shortcut to disconnect the network, the Screen session will not be closed, and it will continue to perform update tasks in the background.

If you want to reconnect to a detached Screen session to see the update process, simply execute the following command:

$screen-r

It is important to note that this command applies only to situations where only one Screen session has been started by the current terminal.

Nested Screen session

Nested Screen sessions, as the name implies, are Screen sessions in Screen sessions, and are one of the ways to enable users to use multiple terminals in one window.

To start a nested Screen session, all you have to do is type the screen command again at the Screen terminal, or press the Ctrl Screen shortcut, which will start another Screen session in your original Screen session, while the original Screen session will remain active in the background.

For example, you run the top command to monitor the system's CPU usage:

$top

Markdown

If you don't want to shut down the top process and want to do other tasks at the same time, you can start a new Screen session using the Ctrl astatc shortcut key, so you can keep the top command running in the background and perform other tasks.

If you want to switch back to the Screen session in which the top command was executed, you can use the shortcut key Ctrl aquin (switch to the next Screen session) or Ctrl aquip (switch to the previous Screen session).

Detach and start a new Screen session

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

For example, you start a Screen session and type df-h to see how the hard drive is being used:

$df-h

Markdown

Then you can press the Ctrl shortcut key to detach from the Screen session and go back to the original terminal. At this point, you can type the screen command again to start a new Screen session to perform other tasks.

If you want to reconnect to the Screen session running the df-h command, you need to detach from the new Screen session and then try to type the screen-r command. At this point, because you have started multiple Screen sessions in parallel, but the screen command does not know which session you want to reconnect to, you will see the following prompt:

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 that the names of both sessions are pts-0.lxlinux, because when I started the Screen session, I didn't specify a name, so they both took the default name as their name.

To reconnect to the Screen session that runs the df-h command, you just need to add the session's ID to the screen-r command, like this:

$screen-r 4352

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

$screen-r alvin

If you don't know the ID and name of the Screen session, you can also check it with the following command:

$screen-ls

Close the Screen session

Closing a Screen session is easy, just like closing a normal session, you can use the Ctrl d shortcut key, or you can type the exit command to close:

$exit

Markdown

Make good use of Screen

In our work, we always encounter tasks that take a long time to complete. If we encounter network outages in the process of implementing these tasks, it will lead to data loss and a waste of time, which will seriously affect our work. Screen solves this problem well by allowing the process to continue to run in the background after the session is disconnected and to resume the process from the breakpoint.

And the screen command also allows us to use a single window to perform multiple tasks at the same time, which is very useful for our work. Therefore, in order to avoid data loss and save time, we should use more screen commands.

At this point, the study of "the use of the screen command in Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report