In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "introduction to the usage of screen command in Linux system". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "introduction to the usage of screen command in Linux system".
As a linux server administrator, you often have to log in to a remote linux machine using ssh to do some time-consuming operations.
You may have come across using telnet or SSH to remotely log in to linux and run some programs. If these programs need to run for a long time (several hours), and a network failure occurs while the program is running, or if the client fails, the link between the client and the remote server will be terminated. and the command that the remote server does not end properly will be forced to terminate.
For example, after you SSH to the host, start a batch of scp commands, if the ssh thread is disconnected, the scp process will be interrupted. Some time-consuming jobs are running on the remote server, but before the work is finished, the operation will be interrupted if you quit. What should I do?
We can solve this problem well by using the screen command. The implementation continues to execute the program on the server with the SSH disconnected.
So what is the screen command?
Screen is called a full-screen window manager, with which you can easily get the effect of multiple virtual terminals on a physical terminal.
Screen feature description:
To put it simply, Screen is a window manager that can multiplex a physical terminal between multiple processes, which means that you can run multi-terminal applications using a single terminal window. There is the concept of session in Screen, where users can create multiple screen windows in a screen session, each screen window as if it were a real telnet/SSH connection window.
Screen command syntax:
Screen [- AmRvx-ls-wipe] [- d] [- h] [- r] [- s] [- S]
Screen command parameters:
-A-[r | R] resizes all windows to the size of the current terminal.
C filename replaces the screen configuration file '.screenrc' with the specified filename file.
-d [pid.tty.host] disconnect the screen process (when using this command, the status of the screen must be Attached, that is, there is a user connected to the screen). The general name of a process is expressed in the form of pid.tty.host (the status can be seen with the screen-list command).
-D [pid.tty.host] performs the same function as the-d command, except that if the execution succeeds, it will kick out the user who was in the screen and let him logout.
-h specifies the number of buffer rows for the window.
-ls or-list displays all current screen jobs.
-m forces the creation of a new screen job even if it is already in the job.
-p number or name pre-selects a window.
-r [pid.tty.host] resume offline screen processes. If there are multiple disconnected processes, you need to specify [pid.tty.host]
-R first attempts to resume offline jobs. If an offline job cannot be found, a new screen job is created.
-s shell specifies the shell to be executed when a new window is created.
-S specifies the name of the screen job. It is used to replace the naming method of [pid.tty.host] and can simplify the operation.
-v displays version information.
-wipe checks all current screen jobs and deletes screen jobs that are no longer available.
-x resumes previously offline screen jobs.
General usage of the Screen command:
Screen-d-r: connect to a screen process. If the process is attached, kick out the remote user before connecting.
Screen-D-r: connect to a screen process. If the process is attached, kick out the remote user and let him logout the connection again.
Screen-ls or-list: shows the existence of screen processes, common commands
Screen-m: if you are in a Screen process, use the shortcut key crtl+a c or type screen directly to create a new window, and screen-m can create a new screen process.
Screen-dm: create a new screen, and default to detached mode, that is, it will not be connected after it is built.
Screen-p number or name: pre-select a window.
Simple steps for Screen to implement background running programs:
1 > when you want to do something, use the command to create a Screen:
The code is as follows:
[linux@user~] $screen-S test1
2 > then you can operate inside. If you need to walk away before your task is finished, use the command to keep Screen:
The code is as follows:
[linux@user~] $Ctrl+a+d # Press Ctrl+a, and then press d to keep Screen
[detached] # this prompt will be displayed, indicating that the Screen has been reserved
If your work is done, type it directly:
The code is as follows:
[linux@user~] $exit # this means you quit successfully
[screen is terminating]
3 > if you kept Screen last time, you can use the command to view:
The code is as follows:
[linux@user~] $screen-ls
There is a screen on:
9649.test1 (Detached)
To restore Screen, use the command:
The code is as follows:
[linux@user~] $screen-r test1 (or 9649)
Shortcut keys used in the Screen command
Ctrl+a c: create window
Ctrl+a w: window list
Ctrl+a n: next window
Ctrl+a p: previous window
Ctrl+a 0-9: switch between window 0 and window 9
Ctrl+a K (uppercase): close the current window and switch to the next window (when exiting the last window, the terminal automatically terminates and returns to the original shell state)
Exit: close the current window and switch to the next window (when exiting the last window, the terminal automatically terminates and returns to the original shell state)
Ctrl+a d: exit the current terminal and return to the status of the shell command before loading screen
Multiple windows
Screen, like many window managers, can support multiple windows. This feature is useful when dealing with multiple tasks without opening a new session at the same time. As a system administrator, I often open four or five SSH sessions at the same time. Under each shell, I may have to deal with two or three tasks. Without screen, you need 15 SSH sessions, 15 logins, 15 windows, and so on. With screen, each system is assigned to a separate session, and I manage different jobs on the system through screen.
To open a new window, simply use "Ctrl-A"c". The new window you create displays a default command prompt. For example, I can run the top command and then open a new window to do other work. Top stays there and runs! You can try it yourself, start screen and run top. (note: I truncated multiple screens to save space. )
Start top
The code is as follows:
Mem: 506028K av, 500596K used, 5432K free
0K shrd, 11752K buff
Swap: 1020116K av, 53320K used, 966796K free
393660K cached
PID USER PRI NI SIZE RSS SHARE STAT CPU ME
6538 root 25 0 1892 1892 596 R 49.1 0.3
6614 root 16 0 1544 1544 668 S 28.3 0.3
7198 admin 15 0 1108 1104 828 R 5.6 0.2
You can now open a new window with "Ctrl-A"c"
The code is as follows:
[admin@ensim admin] $
To get back to top, use "Ctrl-A" n
Mem: 506028K av, 500588K used, 5440K free
0K shrd, 11960K buff
Swap: 1020116K av, 53320K used, 966796K free
392220K cached
PID USER PRI NI SIZE RSS SHARE STAT CPU ME
6538 root 25 0 1892 1892 596 R 48.3 0.3
6614 root 15 0 1544 1544 668 S 30.7 0.3
You can create multiple windows and use "Ctrl-A"n" to switch to the next window, or use "Ctrl-A"p" to return to the previous window. When you are working in another window, every program in the other window will keep running.
Exit screen
There are two ways to exit screen. The first is the same as logging out a shell, you can terminate a window with "Ctrl-A", "K" or "exit". In this way, the current window will be closed, if you have multiple windows open, you will go directly to one of the rest, and if there is only one window, you will exit screen.
Another way to exit screen is to detach the window. This approach simply closes the window but the process is still running. If you have a process that you are sure to execute for a long time, and you need to close the SSH program, you can use "Ctrl-A"d" to detach the window. This will bring you back to shell. All the screen windows are there, and you can take them back later. (translator's note: this is much like minimizing windows and running programs in the background in practice.)
Take-over session
Suppose you are using screen to compile a program for a long time, and suddenly your connection is disconnected. Don't worry, screen will save your compilation progress. After logging back in to your operating system, use the screen list tool to see which sessions are running:
The code is as follows:
[root@gigan root] # screen-ls
There are screens on:
31619.ttyp2.gigan (Detached)
4731.ttyp2.gigan (Detached)
2 Sockets in / tmp/screens/S-root.
Here, I have two different screen sessions. To take over one of them again, use the command of the recovery window:
The code is as follows:
[root@gigan root] # screen-r 31619.ttyp2.gigan
Just use the-r option to add the name of the session, and now you can go back to the screen. The good news is that you can take over again anywhere. Whether in the office or on other clients, you can use screen to start a job and then quit.
Multiple windows
Screen, like many window managers, can support multiple windows. This feature is useful when dealing with multiple tasks without opening a new session at the same time. As a system administrator, I often open four or five SSH sessions at the same time. Under each shell, I may have to deal with two or three tasks. Without screen, you need 15 SSH sessions, 15 logins, 15 windows, and so on. With screen, each system is assigned to a separate session, and I manage different jobs on the system through screen.
To open a new window, simply use "Ctrl-A"c". The new window you create displays a default command prompt. For example, I can run the top command and then open a new window to do other work. Top stays there and runs! You can try it yourself, start screen and run top. (note: I truncated multiple screens to save space. )
Start top
The code is as follows:
Mem: 506028K av, 500596K used, 5432K free
0K shrd, 11752K buff
Swap: 1020116K av, 53320K used, 966796K free
393660K cached
PID USER PRI NI SIZE RSS SHARE STAT CPU ME
6538 root 25 0 1892 1892 596 R 49.1 0.3
6614 root 16 0 1544 1544 668 S 28.3 0.3
7198 admin 15 0 1108 1104 828 R 5.6 0.2
You can now open a new window with "Ctrl-A"c"
The code is as follows:
[admin@ensim admin] $
To get back to top, use "Ctrl-A" n
Mem: 506028K av, 500588K used, 5440K free
0K shrd, 11960K buff
Swap: 1020116K av, 53320K used, 966796K free
392220K cached
PID USER PRI NI SIZE RSS SHARE STAT CPU ME
6538 root 25 0 1892 1892 596 R 48.3 0.3
6614 root 15 0 1544 1544 668 S 30.7 0.3
You can create multiple windows and use "Ctrl-A"n" to switch to the next window, or use "Ctrl-A"p" to return to the previous window. When you are working in another window, every program in the other window will keep running.
Exit screen
There are two ways to exit screen. The first is the same as logging out a shell, you can terminate a window with "Ctrl-A", "K" or "exit". In this way, the current window will be closed, if you have multiple windows open, you will go directly to one of the rest, and if there is only one window, you will exit screen.
Another way to exit screen is to detach the window. This approach simply closes the window but the process is still running. If you have a process that you are sure to execute for a long time, and you need to close the SSH program, you can use "Ctrl-A"d" to detach the window. This will bring you back to shell. All the screen windows are there, and you can take them back later. (translator's note: this is much like minimizing windows and running programs in the background in practice.)
Take-over session
Suppose you are using screen to compile a program for a long time, and suddenly your connection is disconnected. Don't worry, screen will save your compilation progress. After logging back in to your operating system, use the screen list tool to see which sessions are running:
The code is as follows:
[root@gigan root] # screen-ls
There are screens on:
31619.ttyp2.gigan (Detached)
4731.ttyp2.gigan (Detached)
2 Sockets in / tmp/screens/S-root.
Here, I have two different screen sessions. To take over one of them again, use the command of the recovery window:
The code is as follows:
[root@gigan root] # screen-r 31619.ttyp2.gigan
Just use the-r option to add the name of the session, and now you can go back to the screen. The good news is that you can take over again anywhere. Whether in the office or on other clients, you can use screen to start a job and then quit.
Thank you for your reading. the above is the content of "introduction to the usage of screen command in Linux system". After the study of this article, I believe you have a deeper understanding of the introduction of the usage of screen command in Linux system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.