I. BACKGROUND
System administrators often require SSH or telent telnet to a Linux server and often run tasks that take a long time to complete, such as system backups, FTP transfers, and so on. Usually we open a remote terminal window for each of these tasks because they take too long to execute. Must wait for them to complete, during this period cannot shut down the window or disconnects, otherwise this task will be killed, all halfway.
Second, Introduction
GNU Screen is a free software developed by the GNU Program for command-line terminal switching. The software allows the user to connect multiple local or remote command lines simultaneously and freely switch between them. GNU screen can be viewed as a command-line interface version of the window manager. It provides a unified interface for managing multiple sessions and corresponding functions.
Session Recovery
As long as the screen itself is not terminated, the session running inside it can be resumed. This is particularly useful for users who log on remotely-even if the network connection is interrupted, the user will not lose control of the command line session that has already been opened. Once you log on to the host again, execute SCREEN-R to resume the session's operation. Also at the time of the temporary departure, you can perform separate command detach, in order to ensure that the program inside the case of the normal operation of the screen hangs (switch to the background). This is similar to VNC under the graphical interface.
Multi-window
In the screen environment, all sessions run independently and have their own numbering, input, output, and window caches. The user can switch between different windows with shortcut keys, and can freely redirect the input and output of each window. Screen implements basic text operations, such as copy-and-paste, and also provides a scroll bar-like feature to view the history of the window's status. Windows can also be partitioned and named, and can also monitor the activity of background windows.
Session sharing
Screen allows one or more users to log in to a session multiple times from different terminals and share all the features of the session (for example, you can see the exact same output). It also provides a mechanism for window access to password-protect windows.
GNU ' s screen official site: http://www.gnu.org/software/screen/
Third, grammar
screen [-amrvx-ls-wipe][-d < job name >][-h < number of rows >][-r < job name >][-s][-s < job name;]
Parameter description
-A adjusts all windows to the current size of the terminal.
-D < Job name > take the specified screen job offline.
-H < number of rows > specifies the number of buffer rows in the window.
-M forces a new screen job to be established even if the screen job is currently in operation.
-R < Job name > Restore offline screen jobs.
-R first attempt to recover the offline job. If you cannot find an offline job, create a new screen job.
-s Specifies the shell to execute when a new window is created.
-S < job name > Specifies the name of the screen job.
-V Displays version information.
-X the screen job is offline before recovery.
-ls or –list displays all of the current screen jobs.
-wipe checks all current screen jobs and removes screen jobs that are not already available.
Four, commonly used screen parameters
screen -S yourname 新建一个叫yourname的session screen -ls 列出当前所有的session screen -r yourname 回到yourname这个session screen -d yourname 远程detach某个session screen -d -r yourname 结束当前session并回到yourname这个session
Under each screen session, all commands start with Ctrl + A (C-A).
C-a. Display all key binding information
C-a C, create a new window to run the shell and switch to the window
c-a N--Next, switch to the next window
C-a p, Pre vious, switch to the previous window
C-a 0..9, switch to the first 0..9 window
Ctrl + A [Space]--from Windows 0 sequential switch to Windows 9
c-a c-a--two most recent Use the window switch
c-a x, Lock the current window, with the user password to unlock
c-a D-Detach, temporarily leave the current session, the current screen session (may contain multiple WI Ndows) is thrown back into the background and will return to the state that has not yet entered screen, at which point the process (either foreground/background) that runs within each window continues to execute, even if logout is not affected.
C-a Z, put the current session in the background, and use the Shell's FG command to go back.
C-a W-Show All Windows list
C-a T-time, display the current times, and the system's load
c-a k, kill window, forcibly close the current window
C-a [-& Gt To copy mode, you can roll back, search, and copy in copy mode as if using VI
c-b backward,pageup
c-f forward,pagedown
H (uppercase) high, move the cursor to the top left Angle
L Low, move the cursor to the lower left
0 move to the beginning of the line
$
W forward one word, move forward
B backward one word, move backward in words
Space Press for the first time to mark the beginning of the area, the second press the end of
Esc to end copy mode
C-a]-Paste, and then paste the content you just selected in copy mode
V. Use of screen
5.1 Installing screen
A popular Linux distribution, such as Red Hat Enterprise Linux, usually comes with the screen utility, and if not, it can be downloaded from the official website of the GNU screen.
[root@TS-DEV ~]# yum install screen[root@TS-DEV ~]# rpm -qa|grep screenscreen-4.0.3-4.el5[root@TS-DEV ~]#
5.2 Creating a new window
Once the installation is complete, you can start it by tapping the command screen directly. However, this startup screen session does not have a name, in practice it is recommended to take a name for each screens session, convenient to distinguish:
[root@TS-DEV ~]# screen -S david
When screen starts, the first window is created, which is the window No. 0, and in which a system default shell is opened, it will generally be bash. So after you typed the command screen, you immediately returned to the command prompt, as if nothing had happened, but you have entered the world of screen. Of course, you can also add your favorite parameters after the screen command to open the program you specified, for example:
[root@TS-DEV ~]# screen vi david.txt
Screen creates a single-window session that executes VI david.txt, and exiting VI exits the window/session.
5.3 Viewing window and window names
After you open multiple windows, you can use the shortcut key C-a W to list all current Windows. If you use a text terminal, the list is listed in the lower left corner of the screen, and if you use the terminal emulator in the X environment, the list is listed in the title bar. The window list looks like this:
0$ bash 1-$ bash 2*$ bash
In this example, I opened three windows, where the * number indicates that the window 2,-is currently in window 1 when the last time the window was toggled. Screen is named by default as a combination of the number and the name of the running program in the window, which is the default name in the example above. Practice the method of viewing the window above, you may want to have different names for each window to make it easy to distinguish. You can use the shortcut key c-a A To rename the current window, press the shortcut key, screen will allow you to enter a new name for the current window, return to confirm.
5.4 Session Separation and recovery
You can temporarily disconnect (detach) a screen session without interrupting the program running in the screens window, and reconnect (attach) The session at a later time to regain control of the programs running in each window. For example, we open a screen window to edit the/tmp/david.txt file:
[root@TS-DEV ~]# screen vi /tmp/david.txt
Then we want to quit and do something else, like go for a walk, then type c-a d,screen in the screen window to give detached hints:
temporarily interrupt a session
Came back in half an hour and found the screen session:
[root@TS-DEV ~]# screen -ls
Reconnect session:
[root@TS-DEV ~]# screen -r 12865
Everything is in.
Of course, if you don't separate a screen session on another machine, you won't be resuming the session. You can then use the following command to force the session to be detached from its terminal and transferred to the new terminal:
5.5 Clearing Dead Sessions
If for some reason one of the sessions dies (for example, by killing the session), Screen-list will show that the session is dead state. Use the Screen-wipe command to clear the session:
5.6 Closing or killing windows
Normally, when you exit the last program (usually bash) in a window, the window is closed. Another way to close the window is to use C-a K, which kills the current window and kills the running process in the window. If the last window in a screen session is closed, then the entire screens session exits, and the process is terminated. In addition to exiting/killing all windows in the current screen session in turn, you can also use the shortcut key c-a:, and then enter the QUIT command to exit the screens session. It is important to note that this exit kills all windows and exits all programs running in it. In fact, C-a: This shortcut allows users to directly input commands have a lot, including split screen can be input split, which is also a way to implement screen function, but personally think it is more convenient shortcut keys.
Six, screen advanced applications
6.1 Session Sharing
There is also a more fun session recovery that enables session sharing. Suppose you're logging in to a machine with a friend in a different location, and then you create a screen session, and your friend can command it on his terminal:
[root@TS-DEV ~]# screen -x
This command will attach your friend's terminal to your screen session, and your terminal will not be detach. So you can share the same conversation with friends, if you are in the same window now, it is equivalent to sitting in front of the same monitor, your actions will be synchronized to your friends, your friend's operation will be shown to you synchronously. Of course, if you switch to a different window of the session, you can do different things separately.
6.2 Session Lock and unlock
Screen allows the use of shortcut keys C-a s to lock the session. Once locked, any input screen will no longer react. Be aware, though, that your input will be received by the process in screen, although the response is not visible on the screens. Shortcut key c-a Q can unlock a session. You can also use C-a X to lock a session, in which case the session is protected by the password of the user who owns the screen, and the password needs to be entered to continue accessing the session.
6.3 Send command to screen session
Outside of the screen session, a screen session can be manipulated through the screen command, which also adds convenience to using screen as a scripting program. The application of screen in scripting is beyond the scope of getting started, here's an example of how to do a screen outside of a session:
[root@TS-DEV ~]# screen -S sandy -X screen ping www.baidu.com
This command creates a new window in a screen session called Sandy and runs the ping command in it.
6.4 Screen Segmentation
Now that the monitor is so big, it's obviously cool to split a screen into different areas and display different screens. You can use the shortcut key c-a s to split the display horizontally, screen 4.00.03, also support vertical splitter, shortcut key is C-a |. After the split screen, you can use C-A to switch between chunks, and each chunk can create a window and run the process in it. You can use the C-a x shortcut to close the screen block where the current focus is located, or you can use C-A q to close all blocks except the current chunk. The window in the closed chunk is not closed and can also be found by toggling through the window.
6.5 c/p Mode and operation
Another powerful feature of screen is the ability to copy and paste between different windows. Use the shortcut key c-a or c-a [can enter Copy/paste mode, this mode can move the cursor as in VI, and you can use the SPACEBAR to set the marker. In fact, there are many operations like VI in this mode, such as using/searching, using Y to quickly mark a line, using W to quickly mark a word, etc. This section of the documentation for advanced operations in c/p mode is described in more detail.
In general, you can move the cursor to the specified position, press the space to set a start tag, then move the cursor to the end position, press the space to set the second marker, and store the portion between the two markers in Copy/paste buffer and exit Copy/paste mode. In normal mode, you can use the shortcut key C-a] to paste the contents stored in buffer into the current window.
6.6 More screen Features
Like most Unix programs, GNU screen provides rich and powerful customization capabilities. You can specify more in the default level two profile of screen/ETC/SCREENRC and $HOME/.SCREENRC, such as setting screen options, customizing the binding key, setting the screens session self-launch window, enabling multiuser mode, customizing user access control, and more. You can also specify the screen configuration file yourself if you wish.
In the case of multi-user functions, screen is run in single-user mode by default, and you need to specify multiuser on in the configuration file to open multiuser mode via acl* (Acladd,acldel,aclchg ... ) command, you can flexibly configure other users to access your screen session.
Please refer to Screen's man page for more information on the configuration file.
About the use of screen