Detailed explanation of the screen command in CentOS

Source: Internet
Author: User

Detailed explanation of the screen command in CentOS

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 Screen 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.

  • Session recovery
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. You only need to log on to the host again and execute screen-r to resume the session operation. You can also execute the detach command when you leave temporarily to suspend the Screen (switch to the background) when the program in it runs normally ). This is similar to VNC in the graphic interface.
  • Multiple windows
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.
  • Session sharing
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 <job Name>] [-h <number of rows>] [-r <job Name>] [-s] [-S <job Name>]

Parameter description

-A adjusted all windows to the current terminal size.
-D <job Name>: offline the specified screen job.
-H <number of rows> specifies the number of buffer rows in the window.
-M creates a new screen job even if the screen job is already in the job.
-R <job Name> restores 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 <job Name> 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

Then we want to temporarily exit and do something else, such as going out for a walk, then type in the screen windowC-a d, Screen will give 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 the screen, you can use C-a <tab> to switch between blocks. You can create a window on each block and run the process on 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.5C/P mode and Operation

Another powerful function of screen is that it can be copied and pasted between different windows. Use the shortcut key C-a <Esc> or C-a [to enter the copy/paste mode. In this mode, move the cursor as 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, GNUScreen 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.