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
as long as screen itself does not terminate, the session that runs inside it can be recovered. 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
detachto let screen hang (switch to the background) in case the program inside is running normally. This is similar to VNC under the graphical interface.
Multiple Windows
in the 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/
Third, the 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-> 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? ->Show all key binding information
C-a C->Create a new window that runs 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, using the Shell's FG command to go back.
c-a w-> Show 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, you can roll back, search, and copy in copy mode as
c-b with VI BACKWARD,PAGEUP&NBSP
c-f forward,pagedown
H (uppercase) High, move the cursor to the upper-left corner of
L Low, move the cursor to the lower-left corner
0 to the beginning
$ end of line
W forward one word, move forward in words;
B Backward One word, moving backward in words;
space is first pressed as the marker area's starting point and the second time is the endpoint
Esc ends Copy mode
c-a]-> Paste, paste the content you just selected in copy mode with /span>
V. 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 Screenscreen-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.
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:
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 key, screen will allow you to enter a new name for the current window and a 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 go out for a walk, and then type in the screen window C-a d
and give detached tips:
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.
6.6 More screen Features
Like most Unix programs, GNU screen provides rich and powerful customization capabilities. You can specify more in screen's default level two configuration file/ETC/SCREENRC and $HOME/.SCREENRC, such as setting the screens option, customizing the binding key, setting the dialog from Start window, enabling multiuser mode, customizing user access control, and so on. You can also specify the screen profile yourself if you wish.
In the case of multi-user functionality, screen runs in Single-user mode by default, you need to specify multiuser on in the configuration file to open multiuser mode by acl* (Acladd,acldel,aclchg ...). command, you can flexibly configure other users to access your screen session. For more configuration files, refer to the man page on screen.