I. Background
System administrators often need SSH or telent to telnet to a Linux server, often running tasks that take a long time to complete, such as system backup, FTP transfer, and so on. Typically, we open a remote terminal window for each of these tasks, because they take too long to execute. You must wait for them to finish, during which you cannot turn off the window or disconnect, otherwise the task will be killed, and everything should be done by halves. Ii. Introduction
GNU Screen is a free software developed by the GNU Program for command-line terminal switching. This software allows users to simultaneously connect multiple local or remote command sessions and switch between them freely.
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 functionality. Session recovery A session running inside can be recovered as long as the screen itself does not terminate. This is especially useful for users who log on remotely-even if the network connection is interrupted, the user will not lose control of the command session that has been opened. The session can be resumed as long as you log on to the host again to perform screen-r. Also, when you leave temporarily, you can execute the detach command detach to let screen hang (switch to the background) in case the program inside is running normally. This is similar to VNC under the graphical interface. Multi-window in screen environment, all sessions run independently and have their own numbering, input, output, and window caching. Users can switch between different windows by using shortcut keys, and can redirect the input and output of each window freely. Screen implements basic text operations, such as copy and paste, and provides a scroll bar-like feature that allows you to view the history of the window's status. Windows can also be partitioned and named, and can also monitor the activity of the background window. Session sharing screen lets one or more users 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 to the mechanism, you can password protection of the window.
GNU ' s screen official site: http://www.gnu.org/software/screen/ III, Grammar
# screen [-amrvx-ls-wipe][-d < job name >][-h < line number >][-r < job name >][-s][-s < job name]
Parameter description
-A adjusts all windows to the size of the current terminal.
-D < Job name > take the specified screen job offline.
-H < number of rows > The number of buffer lines for the specified window.
-M forces a new screen job to be created even if the screen job is currently in the job.
-R < Job name > Resume Offline screen job.
-R attempts to recover an offline job first. If you cannot find an offline job, create a new screen job.
-s Specifies the shell to be executed when a new window is created.
-S < job name > Specifies the name of the screen job.
-V Displays version information.
-X Offline screen job before recovery.
-ls or--list displays all current screen jobs.
-wipe checks all current screen jobs and deletes screen jobs that are no longer available. Four, the commonly used screen parameters
Screen-s Yourname-> A new session called Yourname
Screen-ls (or screen-list)-> lists all current sessions
Screen-r yourname-> back to yourname this session
screen-d yourname-> Remote Detach a session
Screen-d-R yourname-> ends the current session and returns to Yourname
Under each screen session, all commands begin 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 that window
C-a n-> Next, switch to the next window
C-a p-> Previous, switch to previous window
C-a 0..9-> switch to 0..9 window
CTRL + A [space]-> from Windows 0 sequentially switch to Windows 9
C-a c-a-> Switch between the two most recently used Windows
C-a x-> Lock Current window, unlock with user password
C-a d-> Detach temporarily leaves the current session, throws the current screen session (which may contain multiple windows) to the background, and returns to the state when screen is not in, where each w The process (either foreground/background) running within the Indow continues to execute, even if logout does not affect it.
C-a Z-> The current session to the background, with the Shell's FG command can go back.
C-a w-> Display list of all windows
C-a t-> time, display current times, and system load
c-a k-> Kill window, forcibly closes the current window
c-a [-> into copy mode, in copy mode can be rolled back, search, copy like using VI
C-b Backward,pageup
C-f Forward,pagedown
H (upper case) high, move the cursor to the top left corner
L Low, move the cursor to the lower left corner
0 move to the beginning of the line
$ End of line
W forward one word, moving forward in Word units
b Backward one word, moving backward in words
Space for the first time as the mark of the starting point, the second press as the end point
ESC End Copy Mode
C-A]-> Paste, post the content just selected in copy modev. Use screen
5.1 Installing screen
Popular Linux distributions, such as Red Hat Enterprise Linux, usually come with the screen utility and, if not, can be downloaded from the official website of GNU screen.
[Root@ts-dev ~]# Yum Install screen
[root@ts-dev ~]# rpm-qa|grep screen
screen-4.0.3-4.el5
[Root@ts-dev ~] #
5.2 Creating a new window
Once the installation is complete, just tap the command screen to start it. However, this start screen session has no name, and in practice it is recommended to take a name for each screen session for easy resolution:
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 when you typed the command screen, you immediately returned to the command prompt, as if nothing had happened, but you have already entered the screen world. Of course, you can also add the parameters you like after the screen command to open the program you specify, for example:
[Root@ts-dev ~]# Screen VI david.txt
Screen to create a single window to perform the VI david.txt session, Exit VI will exit the window/session.
To view your own session, you can use the command:
Screen-ls # Or:screen-list
The display is as follows:
Where: 12865 is the ID, that is, the process ID, if named for the session, immediately after the id '. ' The following should be the name of the session, and later the connection can use the ID or its name.
5.3 View window and window name
After you open multiple windows, you can use the shortcut key C-a W to list all of the 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 list of Windows is generally 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 window was switched.
Screen by default gives the window a combination of numbering and running program names in the window, in the example above, where the window is the default name. Practicing the way you look at the window above, you might want to have different names for each window to make it easier to distinguish. You can use the shortcut key c-a A To rename the current window, and when you press the shortcut, screen will allow you to enter a new name for the current window (note: I tried it, I can't. The method of renaming is to C-a and then type: sessionname your_name) Carriage return confirmation.
5.4 Session Separation and recovery
You can temporarily disconnect (detach) the screen session without interrupting the running of the program in the screen window, reconnect (attach) The session later, and 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 for a while and do something else, like going out for a walk, and then typing c-a d,screen in the screen window gives detached hints:
or use
screen-d <session ID or name >
If the local shell appears "cotton", it closes the window itself, but when reconnecting, it finds that the status is "attached" and cannot be connected. At this point, you can also use the above command, interrupt the session, and then reconnect.
Temporarily interrupt session
Back in half an hour, find the screen session:
[Root@ts-dev ~]# Screen-ls
Reconnect session:
[Root@ts-dev ~]# screen-r 12865
Everything is in.
Of course, if you do not detach a screen session on another machine, you cannot resume the session.
You can then use the following command to force the session to detach from its terminal and move it to a new terminal:
5.5 Clear Dead session
If for some reason one of the sessions dies (for example, by killing the session), Screen-list displays the session as a dead state. To clear the session using the Screen-wipe command:
5.6 Close or kill a window
Normally, when you exit the last program (usually bash) in a window, the window closes. 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 turned off, 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 use the shortcut key c-a:, and then enter the QUIT command to exit the screens session. It should be noted that this exit kills all windows and exits all programs running in it. Actually c-a: This shortcut key allows the user to enter directly the command to have many, including the split screen may enter the split and so on, this is also realizes screens the function the way, but personally thinks or the shortcut key is more convenient. Six, screen advanced application
6.1 Session Sharing
There is also a more fun session recovery, you can achieve session sharing. Suppose you are logging on to a machine with a friend in a different location, and then you create a screen session where your friend can command at 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. This allows you to share a conversation with a friend, and if you are currently in the same window, it is equivalent to sitting in front of the same monitor, your operation will be synchronized to your friends, and your friend's operation will be synchronized to you. Of course, if you switch to the different windows of this session, you can do different things separately.
6.2 Session Locking and unlocking
Screen allows c-a s to lock the session using shortcut keys. Once locked, any input screen will not react again. But be aware that although there is no response on the screen, your input will be received by the process in screens. Shortcut key C-a Q unlocks a session.
You can also use C-a X to lock a session, which, when locked, is protected by the password of the user who owns the screen, and you need to enter a password to continue accessing the session.
6.3 Send command to screen session
Outside of the screen session, you can manipulate a screen session through the screen command, which also adds to the convenience of using screen as a scripting program. The application of screen in scripting is beyond the scope of getting started, but here's an example of how you can work with 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 display is so large, it is a cool thing to split a screen into different areas to display different screens. You can use the shortcut key c-a s to split the display horizontally, screen 4.00.03 version, also supports the vertical split, the shortcut is C-a |. After the split screen, you can use c-a <tab> to switch between blocks, and you can create windows and run processes on each block.
You can use the C-a x shortcut key to close the screen block where the current focus is located, or you can use C-A Q to close all but the current block. A window in a closed block is not closed and can be found by a window switch.
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 keys c-a <Esc> or c-a [you can enter Copy/paste mode, which allows you to move the cursor as you would 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/conducting search, using Y to quickly mark a line, using W to quickly mark a word, and so on. For advanced operations in c/p mode, this section of its documentation is described in more detail.
In general, you can move the cursor to the specified position, press the space to set an opening tag, then move the cursor to the end position, press the space to set the second mark, and the two tags between the parts stored in Copy/paste buffer, and exit Copy/paste mode. In normal mode, you can use the shortcut key c-a to paste content stored in buffer into the current window.