Screen command User Guide, screen User Guide
If you ask me what commands can improve my job happiness, I will not hesitate to say, "screen! Yes, it's screen. You must use screen !" As for the cause, listen to the lower part and break it down.
1. Introduction to screen
GNU Screen is a free software developed by GNU program for command line terminal switching. You can use the software to connect multiple local or remote command line sessions at the same time and switch between them freely. GNU Screen can be viewed as the command line interface version of the window manager. It provides a unified interface for managing multiple sessions and corresponding functions. (From Wikipedia)
2. screen function
As a window manager of the command line version, screen mainly has the following functions (the following content is taken from Wikipedia ):
(1) Session recovery
As long as the Screen itself is not terminated, all sessions running inside it can be recovered. This is especially useful for remote login users-even if the network connection is interrupted, the user will not lose control over the opened command line session. You only need to log on to the host again and execute screen-r to resume the session operation. You can also execute the detach command when you leave temporarily to suspend the Screen (switch to the background) when the program in it runs normally ). This is similar to VNC in the graphic interface.
(2) multiple windows
In the Screen environment, all sessions run independently and have their own numbers, inputs, outputs, and window caches. You can switch between different windows by using the shortcut key and redirect the input and output of each window freely. Screen implements basic text operations, such as copying and pasting. It also provides a function similar to the scroll bar to view the historical records of window conditions. The window can also be partitioned and named. You can also monitor the activity of the background window.
(3) Session sharing
Screen allows one or more users to log on to a session multiple times from different terminals and share all the features of the session (for example, you can see identical output ). It also provides a window access permission mechanism to protect the window password.
3. Use instances
This part mainly describes how to use the screen command. In fact, screen is mainly used to run jobs that take a long time. For example, I use an ssh client in windows (I usually use SecureCRT) to connect to the server, and then run a command to run a job. If it takes a long time, the ssh client on windows must be on. Otherwise, the job running on the server will stop. This is a bit annoying, because even if you don't shut down the server and keep running the ssh client, there will still be a tragedy if the network goes wrong. Here is the power of screen.
Screen can manage a series of multiple sessions, and then the job to be run runs under a session. In this way, even if the windows ssh client is closed or the network is faulty, the job running on the server will continue to run as long as the session running on the screen is not closed, in addition, you can reconnect to the server anywhere at any time (such as changing Machines) and connect to the previous session to observe the running status of the job. The following is an example in use.
3.1 Use
For testing, I first write a time-consuming task. for simplicity, I directly write two for loop outputs at intervals of 1 s to run the time-consuming task. The Code is as follows:
#!/bin/bash#file name: work.shfor a in aaa bbb ccc;do echo $a sleep 1donefor((a = 0;a<1000;a++));do echo $a sleep 1done
Then, run the command "chmod a + x work. sh to add the execution permission for the job, so you only need to run the command ". /work. sh.
3.2 run a job in a session
(1) create a session
Run the "screen-S RunWork" command to create a screen session. After the command is executed, a new shell window is displayed, for ease of marking, you can press Ctrl-a A (that is, press Ctrl + a, and then shift + a), so that the cursor in the window will prompt to rename the window. Enter the name "RunWorkWindow" and press enter to rename the window. To avoid confusion between different windows in the same session, you can use the Ctrl-a w shortcut to view the current window name, as shown below:
(2) run the job and session detach and session recovery
Run the "./work. sh" command to run the job. Press Ctrl-a d to detach the current session. In this way, even if the network is disconnected or the ssh client is closed, the job will still run on the server, and you can run the "screen-ls" command to view the current sessions and their respective statuses. If you want to restore a session that has been detach, you only need to use "screen-r SessionID" (SessionID is the number before each session in the result of the screen-ls command ). After the session is restored, you can see the input results and output information of the session on the command line.
At this point, this example is basically over. To run multiple time-consuming jobs at the same time, simply start several more screen sessions.
4. Summary
With screen, we can easily run the job and what should we do. This is the main reason why I say it can improve the happiness index at work.