The background,

System administrators often need SSH or Telent to remotely log in to a Linux server, often running 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. You have to wait for them to complete, and you can’t close the window or disconnect during that time, otherwise the task will be killed and everything will fall by the wayside.

Second, the introduction of

GNU Screen is a free piece of software developed by the GNU Project for command-line terminal switching. The software allows users to simultaneously connect to multiple local or remote command-line sessions and switch between them.

GNU Screen can be viewed as a command-line interface version of the window manager. It provides a unified interface and functionality for managing multiple sessions.

  • Session restore
As long as the Screen itself is not terminated, any session running within it can be resumed. This is especially useful for remotely logged users — even if the network connection is interrupted, the user does not lose control of the command line session that is already open. Just log in to the host again to execute
screen -rYou can resume the session. The separation command can also be executed while temporarily away
detachTo suspend the Screen (switching to the background) while keeping the program running. This is similar to VNC in graphical interface.

  • Many window
In the Screen environment, all sessions run independently and have their own numbering, input, output, and window cache. Users can use shortcut keys to switch between different Windows, and can freely redirect the input and output of each window. Screen implements basic text operations, such as copy and paste; A scrollbar-like feature is also provided to view a history of the state of the window. Windows can also be partitioned and named, and the activity of background Windows can be monitored.

  • Sharing session
Screen allows one or more users to log in to a session multiple times from different terminals, sharing all features of the session (such as seeing the exact same output). It also provides a mechanism for window access and password protection.

GNU’s Screen official site:…

Three, grammar,

[- AmRvx – ls – # screen wiper] [-d < job name >] [-h < number > lines] [-r name > < homework] [-s] [-s < job name >]

Parameters that

-a Resizes all Windows to the current size of the terminal. -d < Job name > takes the specified Screen job offline. -h < Line number > Specifies the number of buffer lines for the window. -m Forces a new Screen job to be created even if the screen job is already in the job. -r < Job name > Restores the offline Screen job. -r first attempts to restore the offline job. If no offline job is found, a new screen job is created. -s specifies the shell to execute when creating a new window. -s < Job name > Specifies the name of the Screen job. -v Displays the version information. -x Restores screen jobs that were previously offline. -ls or –list displays all current screen jobs. -wipe Checks all current screen jobs and removes any screen jobs that are no longer available.

4. Common Screen parameters

Screen -s yourname -> create a new session named yourname screen-ls -> list all current sessions screen -r yourname -> return to yourname session Screen -d yourname -> detach a session screen -d -r yourname -> end the current session and return to yourname session

In each Screen Session, all commands start with CTRL + A (c-a). C-a ? – > show all key bindings information C – a – C – > create a new run shell window and switch to the window C – a n – > Next, switch to the Next window C – a p – > Previous, switch to the Previous window C – a 0.. 9 -> Switch to 0.. 9 Windows Ctrl+a [Space] -> Switch from Window 0 to Window 9 C-a C- A -> Switch between two recently used Windows C-a x -> Lock current window, need to unlock with user password C-a d -> detach, Leave the current session temporarily, throw the current Screen session (which may contain multiple Windows) into the background, and return to the state before you entered screen. At this point in the Screen session, Every process (foreground/background) running in the window continues to execute, even after logout. C-a z -> put the current session in the background, use the shell fg command to go back. C-a w -> display all window list C-a t -> Time, display the current Time, and load C-a k -> kill window, forcibly close the current window C-a [-> Enter the copy mode, In copy mode you can roll back, search, and copy as if 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 line $move to the end of the line w move forward one word B move backward one word Space The first press is the starting point of the marked area, Copy mode C-a] -> Paste Paste the content selected in copy mode

Use screen

5.1 installation screen

Popular Linux distributions, such as Red Hat Enterprise Linux, often come with the Screen utility, and if not, you can download it from the GNU Screen official website.

[root@TS-DEV ~]# yum install screen
[root@TS-DEV ~]# rpm -qa|grep screen4 screen - 4.0.3 - el5 [root @ TS - DEV ~]#Copy the code

5.2 Creating a New Window

When the installation is complete, you can launch it by simply tapping the command screen. However, the Screen session launched this way does not have a name. In practice, it is recommended to give each Screen session a name for easy identification:

[root@TS-DEV ~]# screen -S david Copy the code

When Screen starts, it creates the first window, window No. 0, and opens a system default shell in it, which is usually bash. So you type in “screen” and immediately go back to the command prompt as if nothing had happened, but you’ve already entered the world of screen. Of course, you can also add any argument you like after the screen command to open the program directly. For example:

[root@TS-DEV ~]# screen vi david.txtCopy the code

Screen creates a single window session that executes vi david.txt. Exiting vi will exit the window/session.

5.3 Viewing Windows and Window Names

After multiple Windows are opened, you can use the shortcut key C-A-W to list all the current Windows. If you are using a text terminal, this list is in the lower left corner of the screen, and if you are using a terminal emulator in an X environment, it is in the title bar. The window list will look like this:

0$ bash  1-$ bash  2*$ bash  Copy the code

In this example I have three Windows open, where an asterisk (*) means I am currently in window 2, and a minus (-) means I was in window 1 when I last switched Windows.

By default, Screen names Windows with a combination of the number and the name of the program running in the window. In the example above, the Windows are named by default. After practicing the above method of viewing Windows, you may want to have different names for each window. To rename the current window, use the shortcut key C-a-a. When you press the shortcut key, Screen will allow you to enter a new name for the current window. Press Enter to confirm.

5.4 Session Separation and Recovery

You can detach the screen session temporarily without breaking the programs running in the screen window, and then 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.txtCopy the code

And then we want to quit and do something else, like go for a walk, so type C-A-D in the screen window, and screen will give the detached hint:

Temporarily interrupt a session

I came back half an hour later and found the Screen session:

[root@TS-DEV ~]# screen -lsCopy the code

Reconnect the session:

[root@TS-DEV ~]# screen -r 12865Copy the code

Everything is there.

Of course, if you don’t separate a Screen session on another machine, there’s no way to recover the session.

In this case, you can use the following command to forcibly disconnect the session from its terminal and move it to a new terminal:

5.5 Clearing a Dead Session

If for some reason one of the sessions dies (for example, by killing the session), screen-list will show the session dead. Use the screen-wipe command to clear the session:

5.6 Closing or Killing Windows

Normally, a window closes when you exit the last program (usually bash) in the window. Another way to close a window is to use c-a-k. This shortcut kills the current window and will also kill any processes that are running in that 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 the method of exiting/killing all Windows in sequence in the current Screen session, you can exit the Screen session by using the shortcut C-a: and then typing the quit command. Note that exiting this way will kill all Windows and exit all programs running in them. In fact, c-a: This shortcut key allows the user to directly input a number of commands, including the split Screen can be input split and so on, this is also a way to achieve the Screen function, but I think the shortcut key is more convenient.

Screen advanced applications

6.1 Session Sharing

There is also a more interesting session recovery, which can achieve session sharing. Suppose you are logging in to a machine as the same user from a different location with a friend. Then you create a screen session where your friend can command on his terminal:

[root@TS-DEV ~]# screen -xCopy the code

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 session with your friends, and if you’re in the same window, you’re sitting in front of the same monitor, and your actions are synchronized to your friends, and your friends’ actions are synchronized to you. Of course, if you switch to different Windows of the session, you can still do different things.

6.2 Locking and Unlocking a Session

Screen allows the session to be locked using the c-A-s shortcut key. After locking, the screen will no longer respond to any input. But notice that even though you don’t see anything on the Screen, your input is received by the process on Screen. The shortcut keys C-A-Q can unlock a session.

You can also use c-a-x to lock the session. The difference is that after locking, the session is protected by the password of the user who owns Screen, and you need to enter the password to continue to access the session.

6.3 Sending commands to a Screen session

The ability to manipulate a Screen session outside of a Screen session through the Screen command also facilitates the use of Screen as a script. The use of Screen in scripting is beyond the scope of getting started, but here’s an example of how to use Screen outside of a session:

[root@TS-DEV ~]# screen -S sandy -X screen ping the code

This command creates a new window in a Screen session called Sandy and runs the ping command in it.

6.4 Screen Splitting

With the size of the display, it’s obviously cool to split a Screen into different areas to display different Screen Windows. Can use the shortcut key C – S will display a horizontal segmentation, Screen 4.00.03 version later, also supports the vertical split Screen, keyboard shortcuts is C – | a. After splitting the screen, you can use C-A < TAB > to switch between blocks, and you can create Windows on each block and run processes in it.

You can use the C-a X shortcut to close the area of the screen where the current focus is located. You can also use c-a Q to close all other blocks except the current block. The window in the closed block is not closed and can be found by window toggling.

6.5 C/P mode and operation

Another great feature of Screen is the ability to copy and paste between Windows. You can use c-a <Esc> or c-a [to enter copy/paste mode. You can move the cursor in copy/paste mode just like you would in vi. You can also use the space bar to set the marker. Use W to quickly tag a word, etc. For advanced operations in C/P mode, this section of the document has more detailed instructions.

In general, you can move the cursor to the specified position, press the space to set a starting mark, and then move the cursor to the end position, press the space to set the second mark, while saving the part between the two marks in the Copy /paste buffer, and exit the copy/paste mode. In normal mode, you can use the c-A] shortcut to paste the contents stored in the buffer into the current window.

6.6 More Screen functions

Like most UNIX programs, GNU Screen provides rich and powerful customization capabilities. You can specify more in the default two-level configuration files for Screen, /etc/screenrc and $HOME/. Screenrc, such as setting Screen options, customizing binding keys, setting Screen session auto-start Windows, enabling multi-user mode, customizing user access control, and so on. You can also specify your own Screen profile if you wish.

For example, screen runs in single-user mode by default. To enable multi-user mode, you need to specify multiuser on in the configuration file, and use the acl* (acladd,acldel,aclchg… Command, you can flexibly configure other users to access your Screen session. Refer to the Man page of Screen for more configuration file information.

Reprinted from:…