The system administrator often needs to remotely log on to the Linux server through SSH or telent, and often runs tasks that take a long time to complete, such
I. Background
The system administrator often needs to remotely log on to the Linux server through SSH or telent, and often runs tasks that take a long time to complete, such as system backup and ftp transmission. Generally, we open a remote terminal window for each of these tasks because they have been executed for too long. You must wait until they are completed. during this period, you cannot close the window or disconnect the connection. Otherwise, the task will be killed and everything will be abandoned.
II. Introduction
GNU ScreenIt 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.
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. As long as you log on to the host again for execution
Screen-rThe session can be resumed. You can also execute the separation command when you leave temporarily.
DetachTo suspend the Screen (switch to the background) while ensuring the normal operation of the program ). This is similar to VNC in the graphic interface.
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.
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.
GNU's Screen official site: http://www.gnu.org/software/screen/
III. Syntax
# Screen [-AmRvx-ls-wipe] [-d <作业名称> ] [-H <行数> ] [-R <作业名称> ] [-S] [-S <作业名称> ]
Parameter description
-A adjusted all windows to the current terminal size.
-D <作业名称> Offline the specified screen job.
-H <行数> Specify the number of buffer lines in the window.
-M creates a new screen job even if the screen job is already in the job.
-R <作业名称> Restore an offline screen job.
-R first tries to restore the offline job. If an offline job cannot be found, a new screen job is created.
-S specifies the shell to be executed when a new window is created.
-S <作业名称> Specifies the name of the screen job.
-V displays the version information.
-X restores the offline screen job.
-Ls or -- list displays all current screen jobs.
-Wipe checks all current screen jobs and deletes unusable screen jobs.
IV. common screen parameters
Screen-S yourname-> Create a session named yourname
Screen-ls-> List all current sessions
Screen-r yourname-> Return to yourname session
Screen-d yourname-> remote detach a session
Screen-d-r yourname-> end the current session and return to yourname.
In each screen session, all commands start with ctrl + a (C-.
C-? -> Display all key binding information
C-a c-> create a new window for running shell and switch to it
C-a n-> Next, switch to the Next window
C-a p-> Previous, switch to the Previous window
C-a 0 .. 9-> switch to 0th .. 9 Windows
Ctrl + a [Space]-> switch from window 0 sequentially to window 9
C-a-> switch between the two recently used Windows
C-a x-> lock the current window and use the user password to unlock it.
C-a d-> detach: temporarily leaves the current session, throwing the current screen session (which may contain multiple windows) to the background for execution, and returning to the status when the screen is not yet in progress, at this time, in the screen session, the process running in each window (both the foreground and background) continues to be executed, even if logout is not affected.
C-a z-> put the current session in the background for execution, and use the shell fg command to go back.
C-a w-> Display the list of all Windows
C-a t-> Time: displays the current Time and the system load
C-a k-> kill window, force close the current window
C-a [-> enter the copy mode. in the copy mode, you can roll back, search, and copy the file, just like using vi.
C-B Backward, PageUp
C-f Forward, PageDown
H (uppercase) High: Move the cursor to the upper left corner
L Low: Move the cursor to the lower left corner.
0 move to the beginning of the row
$ End of line
W forward one word, in the unit of words forward
B backward one word, move backward in the unit of words
Space is the start point of the marked area for the first time, and Space is the end point for the second time
Esc end copy mode
C-a]-> Paste, Paste the content just selected in copy mode
5. use screen
5.1 install screen
Popular Linux distributions (such as Red Hat Enterprise Linux) usually come with screen utilities. if they do not exist, they can be downloaded from the GNU screen official website.
[root@TS-DEV ~]# yum install screen[root@TS-DEV ~]# rpm -qa|grep screenscreen-4.0.3-4.el5[root@TS-DEV ~]#
5.2 create a new window
After the installation is complete, Press screen to start it. However, the screen session started in this way has no name. In practice, we recommend that you set a name for each screen session to facilitate resolution:
[root@TS-DEV ~]# screen -S david
After screen is started, the first window, that is, window No. 0, is created, and a default shell of the system is opened, usually bash. So after you press the screen command, you will immediately return to the command prompt, as if nothing happened. In fact, you have already entered the Screen world. Of course, you can also add your favorite parameters after the screen command to directly open your specified program, for example:
[root@TS-DEV ~]# screen vi david.txt
Screen creates a single window session for executing vi David .txt. exiting vi will exit the window/session.
5.3 view the window and window name
After opening multiple windows, you can use the shortcut key C-a w to list all Windows currently. If you use a text terminal, this list is listed in the lower left corner of the screen. if you use a terminal simulator in the X environment, this list is listed in the title bar. The window list looks like this:
0$ bash 1-$ bash 2*$ bash
In this example, three windows are opened, where "*" indicates that the current window is located at "2", and "-" indicates that the last window is located at "window 1.
By default, Screen will name the window as a combination of the number and the name of the running program in the window. in the above example, the window is the default name. After practicing the window display method above, you may want different windows to have different names for easy differentiation. You can use the shortcut key C-a A to rename the current window. after you press the shortcut key, Screen allows you to enter a new name for the current window and press enter to confirm.
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, and re-connect (attach) the session at a later time to re-control the program running in each window. For example, open a screen window to edit the/tmp/David. txt file:
[root@TS-DEV ~]# screen vi /tmp/david.txt
After that, we want to temporarily exit and do something else, such as going out for a walk, then type C-a d in the screen window, and Screen will display the detached prompt:
Temporarily interrupt session
After half an hour, I came back 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 do not separate a Screen session on another machine, you will not be able to recover the session.
In this case, you can use the following command to force the session to be detached from the terminal where it is located and transferred to the new terminal:
5.5 clear dead sessions
If one of the sessions is killed for some reason (for example, manually killing the session), screen-list displays the session as dead. Run the screen-wipe command to clear the session:
5.6 close or kill the window
Normally, when you exit the last program in a window (usually bash), the window is closed. Another way to close the window is to use C-a k. This shortcut will kill the current window and also kill the running process in the window.
If the last window in a Screen session is closed, the entire Screen session exits and the screen process is terminated.
In addition to exiting/killing all windows in the current Screen session in sequence, you can also use the shortcut key C-a: and then enter the quit command to exit the Screen session. It should be noted that such exit will kill all windows and exit all programs running in the window. In fact, C-a: the shortcut key allows you to directly enter many commands, including split for split Screen, which is also a way to implement the Screen function, however, I personally think it is more convenient to use shortcuts.
VI. screen advanced applications
6.1 session sharing
There is also a more fun way to recover sessions to achieve session sharing. Assume that you are logged on to a machine with the same user in different locations with your friends, and then you create a screen session. your friend can run the following command 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. In this way, you can share the same session with your friends. if you are in the same window, it is equivalent to sitting in front of the same monitor, your operations will be synchronously presented to your friends, and your operations will also be displayed to you. Of course, if you switch to different windows of this session, you can perform different operations separately.
6.2 session lock and unlock
Screen allows you to use the shortcut key C-a s to lock the session. After the screen is locked, no response will be made to any input screen. However, you must note that even if no response is displayed on the Screen, your input will be received by the process in the Screen. The shortcut key C-a q can unlock a session.
You can also use C-a x to lock the session. The difference is that the session will be protected by the password of the user to which the Screen belongs. you need to enter the password to continue accessing the session.
6.3 send command to screen session
In addition to Screen sessions, you can use the screen command to operate a Screen session, which also makes it easier to use Screen as a script program. The application of Screen in the script is out of the scope of getting started. here we only take a look at one example to understand the operations on Screen outside the 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.
6.4 screen split
Now the display is so big, it is really cool to split a Screen into different areas to display different Screen windows. You can use the shortcut key C-a S to split the display horizontally. after Screen 4.00.03, vertical split is also supported. the shortcut key is C-a |. After split screen, you can use C- Switch between blocks. you can create a window for each block 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 use C-a Q to close all blocks except the current block. The window in the closed block is not closed. you can also find it through window switching.
6.5 C/P mode and operations
Another powerful function of screen is that it can be copied and pasted between different windows. Use the shortcut key C- Or C-a [can enter the copy/paste mode. In this mode, you can move the cursor like in vi and use the space key to set the flag. In this mode, there are many operations similar to vi, such as using/for search, using y to quickly mark a row, and using w to quickly mark a word. This section describes the advanced operations in C/P mode.
Generally, you can move the cursor to the specified position, press a space to set a start mark, move the cursor to the end, and press a space to set the second mark, at the same time, the parts between the two tags are stored in copy/paste buffer, and the copy/paste mode is exited. In normal mode, you can use the shortcut key C-a] to paste the content stored in the buffer to the current window.
6.6 more screen functions
Like most UNIX programs, GNU Screen provides rich and powerful customization functions. You can configure the files/etc/screenrc and $ HOME/by default in Screen /. specify more in screenrc, such as setting screen options, customizing binding keys, setting screen session self-start windows, enabling multi-user mode, and customizing user access permission control. If you want to, you can also specify the screen configuration file.
Taking the multi-user function as an example, screen runs in single-user mode by default. you need to specify multiuser on in the configuration file to enable multi-user mode, through acl * (acladd, acldel, aclchg ...) command, you can flexibly configure other users to access your screen session. For more information about the configuration file, see the man page of screen.