System administrators often need SSH or teletel to log in to a Linux server remotely, and often run tasks that take a long time to complete, such as system backup, ftp transfer, and so on. Normally we open a remote terminal window for each of these tasks because they take too long to execute. You must wait for them to complete. During this period, you cannot close the window or disconnect, otherwise this task will be killed and everything will be abandoned halfway.
GNU计划开发的用于命令行终端切换的自由软件。 GNU Screen is a free software developed by the GNU Project for command line terminal switching . 会话，并在其间自由切换。 The software allows users to connect to multiple local or remote command-line sessions simultaneously and switch between them.
GNU Screen can be thought of as a command-line interface version of the window manager. It provides a unified interface and corresponding functions for managing multiple sessions.
GNU's Screen official site: http://www.gnu.org/software/screen/
Third, the grammar
# screen [-AmRvx -ls -wipe] [-d <job name>] [-h <number of lines>] [-r <job name>] [-s] [-S <job name>]
-A Resize all windows to the size of the current terminal.
-d <job name> Takes the specified screen job offline.
-h <number of lines> specifies the number of buffer lines in the viewport.
-m Forces the creation of a new screen job even if the screen job is currently in operation.
-r <job name> Resume the offline screen job.
-R first attempts to resume an offline job. If no offline assignment is found, create a new screen assignment.
-s specifies the shell to execute when creating a new window.
-S <job name> specifies the name of the screen job.
-v Display version information.
-x Resume screen jobs that were previously offline.
-ls or --list display all current screen jobs.
-wipe Check all current screen jobs and delete screen jobs that are no longer available.
Fourth, commonly used screen parameters
screen -S yourname-> create a new session called yourname
screen -ls-> list all current sessions
screen -r yourname-> back to yourname session
screen -d yourname-> detach a session remotely
screen -d -r yourname-> end the current session and return to yourname session
In each screen session, all commands begin with ctrl + a (Ca).
Ca?-> Show all key binding information
Ca c-> create a new window to run the shell and switch to that window
Ca n-> Next, switch to the next window
Ca p-> Previous, switch to the previous window
Ca 0..9-> switch to 0..9 window
Ctrl + a [Space]-> Sequentially switch from window 0 to window 9
Ca Ca-> switch between two recently used windows
Ca x-> lock the current window, need to unlock with user password
Ca d-> detach, leave the current session temporarily, throw the current screen session (may contain multiple windows) to the background for execution, and return to the state before the screen is entered. At this time, in the screen session, each window The internal processes (both foreground and background) continue to execute, even if logout is not affected.
Ca z-> put the current session in the background for execution, you can go back with the shell's fg command.
Ca w-> show a list of all windows
Ca t-> Time, displays the current time, and the system load
Ca k-> kill window, forcibly close the current window
Ca [-> Enter copy mode, in copy mode, you can roll back, search, and copy just like using vi
Cb Backward, PageUp
Cf 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 line
$ End of line
w forward one word, move forward in words
b backward one word, backward in words
The first press of Space is the start of the marked area, and the second press is the end
Esc ends copy mode
Ca]-> Paste, paste the content you just selected in copy mode
Five, use screen
5.1 install screen
Popular Linux distributions (such as Red Hat Enterprise Linux) usually come with a screen utility. If not, you can download it from the official GNU screen website.
[root @ TS-DEV ~] # yum install screen [root @ TS-DEV ~] # rpm -qa | grep screen screen-4.0.3-4.el5 [root @ TS-DEV ~] #
5.2 Create a new window
After the installation is complete, just type the command screen to start it. However, the screen session started in this way does not have a name. In practice, it is recommended to choose a name for each screen session to facilitate identification:
[root @ TS-DEV ~] # screen -S david
After screen starts, it will create the first window, namely window No. 0, and open a system default shell in it, which will usually be bash. So after you type the command screen, you will immediately return to the command prompt, as if nothing happened. In fact, you have entered the world of Screen. Of course, you can also add the parameters you like after the screen command to directly open the program you specified, for example:
[root @ TS-DEV ~] # screen vi david.txt
screen creates a single window session executing vi david.txt, exiting vi will exit that window / session.
5.3 View window and window name
After opening multiple windows, you can use the shortcut key Ca w to list all the current windows. If you use a text terminal, this list will be listed in the lower left corner of the screen. If you use a terminal emulator in the X environment, this list will be 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 * sign indicates that the window is currently in window 2 and the-sign indicates that the window was in window 1 when the window was last switched.
Screen defaults the window to a combination of the number and the name of the running program in the window. In the above example, the windows are the default names. After practicing the method of viewing windows above, you may wish that each window could have a different name to facilitate the distinction. You can use the shortcut Ca A to rename the current window. After pressing the shortcut, Screen will allow you to enter a new name for the current window and press Enter to confirm.
5.4 Session separation and recovery
You can temporarily detach the screen session without interrupting the running of the programs in the screen window, and reattach the session at a later time to re-control 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
Ca d ，Screen会给出detached提示： Afterwards, we want to temporarily quit doing something else, such as going for a walk, then type
Ca d in the screen window , and Screen will give a detached prompt:
Temporarily interrupt the session
Came back after half an hour and found the screen session:
[root @ TS-DEV ~] # screen -ls
Reconnect the session:
[root @ TS-DEV ~] # screen -r 12865
Everything is there.
Of course, if you don't detach a Screen session on another machine, you won't be able to resume the session.
At this time, you can use the following command to forcibly detach the session from its terminal and transfer it to a new terminal:
5.5 Clearing dead sessions
If one of the sessions dies for some reason (such as killing the session artificially), then screen -list will display the session as dead. Use the screen -wipe command to clear the session:
5.6 Closing or killing the window
Normally, when you exit the last program (usually bash) in a window, the window is closed. Another way to close a window is to use Ca k. This shortcut will kill the current window and will also kill the running process in this window.
If the last window in a Screen session is closed, the entire Screen session is exited and the screen process is terminated.
In addition to the method of quitting / killing all windows in the current Screen session in sequence, you can also use the shortcut key Ca: and then enter the quit command to exit the Screen session. Note that exiting this way will kill all windows and exit all programs running in them. Actually Ca: This shortcut key allows users to directly input a lot of commands, including split screen can enter split, etc. This is also a way to achieve the Screen function, but personally think that shortcut keys are more convenient.
Six, screen advanced applications
6.1 Session sharing
There is also a more interesting session recovery, which can realize session sharing. Suppose you are logging in to a machine with the same user in different places with a friend, and then you create a screen session, your friend can 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 currently in the same window, it is equivalent to sitting in front of the same display. Your operations will be demonstrated to your friends and your friends' operations will also be synchronized Give you. Of course, if you switch to different windows of this session, you can still perform different operations separately.
6.2 Session lock and unlock
Screen allows the session to be locked using the shortcut Cas. Once locked, no further response to any input screen. But note that although no response can be seen on the screen, your input will be received by processes in the Screen. The shortcut Ca q unlocks a session.
You can also use Ca x to lock the session. The difference is that after this is locked, the session will be protected by the password of the user to which Screen belongs. You need to enter the password to continue accessing the session.
6.3 Sending Commands to a Screen Session
In addition to the Screen session, you can use the screen command to operate a Screen session, which also increases the convenience of using Screen as a script program. Regarding the application of Screen in the script is beyond the scope of entry. Here is just an example to experience the operation of 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 in it.
6.4 Screen split
Now that the display is so large, it is obviously cool to split a screen into different areas to display different Screen windows. You can use the shortcut key Ca S to split the display horizontally. After Screen 4.00.03, it also supports vertical split screen. The shortcut key is Ca |. After splitting the screen, you can use Ca <tab> to switch between the various blocks, and you can create windows and run processes in each block.
You can use the Ca X shortcut key to close the screen block where the current focus is located, or you can use Ca Q to close all blocks except the current block. The window in the closed block does not close, you can also find it by window switching.
6.5 C / P mode and operation
Another very powerful feature of screen is that you can copy and paste between different windows. Use the shortcut keys Ca <Esc> or Ca [to enter the copy / paste mode. In this mode, you can move the cursor like in vi, and you can use the space bar to set the mark. In fact, there are many vi-like operations in this mode, such as using / to search, use y to quickly mark a line, and use w to quickly mark a word. About the advanced operation in C / P mode, this part of the document has a more detailed description.
In general, you can move the cursor to the specified position, press the space to set an opening mark, and then move the cursor to the end, press the space to set the second mark. At the same time, the part between the two marks is stored in copy / paste buffer and exit copy / paste mode. In normal mode, you can use the shortcut key Ca] to paste the content stored in the buffer into the current window.
6.6 More screen functions
Like most UNIX programs, GNU Screen provides a wealth of powerful customization features. You can specify more in the screen's default two-level configuration files / etc / screenrc and $ HOME / .screenrc, such as setting screen options, customizing binding keys, setting screen session auto-launch windows, enabling multi-user mode, customizing User access control and more. If you prefer, you can also specify the screen configuration file yourself.
Take the multi-user function as an example, the screen runs in single-user mode by default. You need to specify multiuser on in the configuration file to enable multi-user mode. With the acl * (acladd, acldel, aclchg ...) command, you can be flexible. Configure other users to access your screen session. For more configuration file contents, please refer to the man page of screen.