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

How to use Screen in linux

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

Share

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

This article mainly shows you "how to use Screen in linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Screen in linux" this article.

Screen is a command-line terminal tool developed by GNU that provides connections to the same shell session (session sharing) from multiple terminal windows. When the network is interrupted, or the terminal window closes unexpectedly, the program running in the screen can still run (the system's own terminal window, when the window closes unexpectedly, the program running in the terminal window will also be terminated. ).

Install screen tools

It@serverc:~$ sudo apt install screen-y

* in CentOS, you need to install epel-release before you can install screen (use yum or dnf to install software in CentOS)

Session sharing

Create a screen session named it on host A

It@serverc:~$ screen-S it

On host B, connect to host A via ssh and check the existing screen sessions through the-ls option

It@workstation:~/ansible$ ssh it@10.10.10.108 it@10.10.10.108's password: Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-60-generic x86 * 64). Last login: Thu Jan 14 13:57:32 2021 it@serverc:~$ screen-ls There is a screen on: 2107.it (01 it@serverc:~$ screen 14 + 21 07:30:27) (Attached) 1 Socket in / run/screen/S-it.

Then join the session

It@serverc:~$ screen-x it

This allows both sides to share a screen session, and no matter what command the other runs, the other can see

When you need to exit the session, you can run exit to exit the session (when you run exit, both sides will exit the screen session. Or via Ctrl+A,D (Ctrl+An and then press D to exit a single session).

When the network is interrupted or the window closes unexpectedly, the task continues to run

Open two terminal windows in Linux, which our branch calls window An and window B, and run ping in window A.

It@workstation:~$ ping 10.10.10.1 PING 10.10.10.1 (10.10.10.1) 56 (84) bytes of data. 64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=1.21 ms 64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=1.22 ms 64 bytes from 10.10.10.1: icmp_seq=3 ttl=64 time=1.19 ms......

Then in the B window, run ps aux to get the system process, and then filter through grep

It@workstation:~$ ps aux | grep ping it 2336 0.00.2 317144 8740? Ssl 2020 0:17 / usr/libexec/gsd-housekeeping it 153585 0.0 12764 872 pts/3 S+ 14:53 0:00 ping 10.10.10.1 it 153646 0.0 12108 736 pts/2 S+ 14:56 grep-- color=auto ping

We see ping running

When we close window A, we go to window B (there is only one window now) and run the previous ps aux command

It@workstation:~$ ps aux | grep ping it 2336 0.00.2 317144 8740? Ssl 2020 0:17 / usr/libexec/gsd-housekeeping it 153651 0.0 12108 2964 pts/2 S + 14:57 0:00 grep-- color=auto ping

At this point, the ping command is over.

Reopen a new terminal window, as window A, and run screen

It@serverc:~$ screen-S test

Then run the ping command in the screen window

It@workstation:~$ ping 10.10.10.1 PING 10.10.10.1 (10.10.10.1) 56 (84) bytes of data. 64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=1.21 ms 64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=1.15 ms 64 bytes from 10.10.10.1: icmp_seq=3 ttl=64 time=1.21 ms......

Then close window A, go back to window B, and run the ps aux command

It@workstation:~$ ps aux | grep ping it 2336 0.00.2 317144 8684? Ssl 2020 0:17 / usr/libexec/gsd-housekeeping it 154964 0.0 12768 940 pts/3 S + 15:02 0:00 ping 10.10.10.1 it 154986 0.0 12108 736 pts/4 S + 15:05 grep-- color=auto ping

We found that the ping command is still running

When we want to go back to the previous ping session, we can resume the session through screen-r

It@workstation:~$ screen-r test

* if there is only one screen session, the following session name can be omitted. If you don't know, or forget the session command, you can check the current host running screen via screen-ls.

Upper and lower screen

Create a screen session

It@serverc:~$ screen-S test

Press Ctrl+A, then press Shift+S to open the split screen.

Press Ctrl+A, then press Tab to switch to the following screen

But at this point, there is nothing on the screen below. You need to go through Ctrl+A, and then press C to create the terminal in the screen below.

So we can execute the command on the following screen, and if we need to switch between different screens, we can switch by pressing Ctrl+An and then pressing Tab.

When we need to exit, we can exit with the exit command (after running the exit command on one screen, the terminal exits, but the split state does not exit, so you need to switch to another screen and run exit to exit again by the previous method).

The above is all the content of the article "how to use Screen in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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: 283

*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