Screen session Command Linux

Source: Internet
Author: User
Tags session id sessions

Remember very early already wrote about screen of article, somehow lost, had to write again, convenient later use!

Due to the frequent running of programs on the server, it is not possible for the local to remain connected to the server, and if the local and server connections are broken, the program running on the server will be terminated, in order to find some network information, found that the screen session command can keep local and server disconnected, The program continues to run on the server, and after the run is finished, output the final result.

Because again write a comparison wasted time, here directly forwards David_tang article, very whole! ( some of the following themselves have been partially added depending on the problem encountered )

I. Background

System administrators often require SSH or telent telnet to a Linux server and often run 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. Must wait for them to complete, during this period cannot shut down the window or disconnects, otherwise this task will be killed, all halfway.

Second, Introduction

GNU Screen is a free software developed by the GNU Program for command-line terminal switching. The software allows the user to connect multiple local or remote command lines simultaneously and freely switch between them.

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

    • Session Recovery
as long as the screen itself is not terminated, the session running inside it can be resumed. This is particularly useful for users who log on remotely-even if the network connection is interrupted, the user will not lose control of the command line session that has already been opened. Once you log on to the host again, execute screen-r to resume the session's operation. Also at the time of the temporary departure, you can perform separate command detach, in order to ensure that the program inside the case of the normal operation of the screen hangs (switch to the background). This is similar to VNC under the graphical interface.
    • Multi-window
in the screen environment, all sessions run independently and have their own numbering, input, output, and window caches. The user can switch between different windows with shortcut keys, and can freely redirect the input and output of each window. Screen implements basic text operations, such as copy-and-paste, and also provides a scroll bar-like feature to view the history of the window's status. Windows can also be partitioned and named, and can also monitor the activity of background windows.
    • Session sharing
Screen allows
one or more users to log in to a session multiple times from different terminals and share all the features of the session (for example, you can see the exact same output). It also provides a mechanism for window access to password-protect windows.

GNU ' s screen official site: http://www.gnu.org/software/screen/

Third, grammar

# screen [-amrvx-ls-wipe][-d < job name >][-h < number of lines >][-r < job name >][-s][-s < job name;]

Parameter description

-A adjusts all windows to the current size of the terminal.
-D < Job name > take the specified screen job offline.
-H < number of rows > specifies the number of buffer rows in the window.
-M forces a new screen job to be established even if the screen job is currently in operation.
-R < Job name > Restore offline screen jobs.
-R first attempt to recover the offline job. If you cannot find an offline job, create a new screen job.
-s Specifies the shell to execute when a new window is created.
-S < job name > Specifies the name of the screen job.
-V Displays version information.
-X the screen job is offline before recovery.
-ls or--list displays all of the current screen jobs.
-wipe checks all current screen jobs and removes screen jobs that are not already available.

Four, commonly used screen parameters

Screen-s Yourname, a new session called Yourname
Screen-ls (or screen-list), list all current session
Screen-r yourname, back to yourname this session
screen-d yourname, remote detach a session
Screen-d-R Yourname End the current session and return to the session Yourname

under each screen session, all commands start with Ctrl + A (C-A).
C-a? Show all key binding information
C-a C-Create a new window to run the shell and switch to that 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 the first 0..9 window
CTRL + A [Space], from Windows 0 sequential switch to Windows 9
C-a c-a, switching between the two most recently used Windows
C-a x, lock the current window and unlock it with the user's password
C-a D-Detach, temporarily leave the current session, the current screen session (may contain multiple windows) to the background to execute, and will return to the status of not yet in screen, at this time in the screen session, each W The process running within the Indow (either foreground/background) continues to execute, even if logout is not affected.
C-a Z, put the current session in the background, and use the Shell's FG command to go back.
C-a W-Show All Windows list
C-a T-time, showing the current times, and the system's load
Kill window, C-a K, forcibly closes the current window
c-a [, enter copy mode, can be rolled back, searched, copied in copy mode 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
$ End of line
W forward one word, move forward in words
b Backward One word, move backward in words
Space is first pressed as the starting point for the marked area, and the second press is the end point
ESC ends Copy Mode
C-A], Paste, paste the content you just selected in copy mode

v. Use of screen

5.1 Installing screen

A popular Linux distribution, such as Red Hat Enterprise Linux, usually comes with the screen utility, and if not, it can be downloaded from the official website of the GNU screen.

[[email protected] ~]# yum install screen[[email protected] ~]# rpm-qa|grep Screenscreen-4.0.3-4.el5[[email protected] ~] #

5.2 Creating a new window

Once the installation is complete, you can start it by tapping the command screen directly. However, this startup screen session does not have a name, in practice it is recommended to take a name for each screens session, convenient to distinguish:

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 after you typed the command screen, you immediately returned to the command prompt, as if nothing had happened, but you have entered the world of screen. Of course, you can also add your favorite parameters after the screen command to open the program you specified, for example:

[Email protected] ~]# Screen VI david.txt

Screen creates a single-window session that executes VI david.txt, and exiting VI exits the window/session.

To view your own session, you can use the command:

Screen-ls  # Or:screen-list

Shown below:

Where: 12865 is the ID, that is, the ID of the process, if the session is named, immediately after the id '. ' The following should be the name of the session, the next connection can use the ID or the name of their own names.

5.3 Viewing window and window names

After you open multiple windows, you can use the shortcut key C-a W to list all 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 window list looks like this:

0$ bash  1-$ bash  2*$ bash  

In this example, I opened three windows, where the * number indicates that the window 2,-is currently in window 1 when the last time the window was toggled.

Screen is named by default as a combination of the number and the name of the running program in the window, which is the default name in the example above. Practice the method of viewing the window above, you may want to have different names for each window to make it easy to distinguish. You can use the shortcut key c-a A To rename the current window, press the shortcut key, screen will allow you to enter a new name for the current window, return to confirm.

5.4 Session Separation and recovery

You can temporarily disconnect (detach) a screen session without interrupting the program running in the screens window, and reconnect (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:

[Email protected] ~]# screen Vi/tmp/david.txt

Then we want to quit for a while and do something else, like go for a walk, then type in the screen window C-a d , which will give you a detached hint:

or use

screen-d  <session ID or name >

If the local shell appears "stuck", it closes the window itself, but when reconnecting, it discovers that the status is "attached" and cannot be connected. At this point, you can also use the above command, first interrupt the session, and then reconnect.

Temporarily interrupt a session

Came back in half an hour and found the screen session:

[Email protected] ~]# Screen-ls

Reconnect session:

[Email protected] ~]# screen-r 12865

Everything is in.

Of course, if you don't separate a screen session on another machine, you won't be resuming the session.

You can then use the following command to force the session to be detached from its terminal and transferred to the new terminal:

5.5 Clearing Dead Sessions

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

5.6 Closing or killing windows

Normally, when you exit the last program (usually bash) in a window, the window is closed. 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 closed, then 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 also use the shortcut key c-a:, and then enter the QUIT command to exit the screens session. It is important to note that this exit kills all windows and exits all programs running in it. In fact, C-a: This shortcut allows users to directly input commands have a lot, including split screen can be input split, which is also a way to implement screen function, but personally think it is more convenient shortcut keys.

Six, screen advanced applications

6.1 Session Sharing

There is also a more fun session recovery that enables session sharing. Suppose you're logging in to a machine with a friend in a different location, and then you create a screen session, and your friend can command it on his terminal:

[Email protected] ~]# screen-x

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 conversation with friends, if you are in the same window now, it is equivalent to sitting in front of the same monitor, your actions will be synchronized to your friends, your friend's operation will be shown to you synchronously. Of course, if you switch to a different window of the session, you can do different things separately.

6.2 Session Lock and unlock

Screen allows the use of shortcut keys C-a s to lock the session. Once locked, any input screen will no longer react. Be aware, though, that your input will be received by the process in screen, although the response is not visible on the screens. Shortcut key c-a Q can unlock a session.

You can also use C-a X to lock a session, in which case the session is protected by the password of the user who owns the screen, and the password needs to be entered to continue accessing the session.

6.3 Send command to screen session

Outside of the screen session, a screen session can be manipulated through the screen command, which also adds convenience to using screen as a scripting program. The application of screen in scripting is beyond the scope of getting started, here's an example of how to do a screen outside of a session:

[Email protected] ~]# 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 monitor is so big, it's obviously cool to split a screen into different areas and display different screens. You can use the shortcut key c-a s to split the display horizontally, screen 4.00.03, also support vertical splitter, shortcut key is C-a |. After the split screen, you can use c-a <tab> to switch between blocks, and each chunk can create a window 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 you can use C-A q to close all blocks except the current chunk. The window in the closed chunk is not closed and can also be found by toggling through the window.

6.5 c/p Mode and operation

Another powerful feature of screen is the ability to copy and paste between different windows. Use shortcut keys c-a <Esc> or C-a [can enter Copy/paste mode, this mode can move the cursor as 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/searching, using Y to quickly mark a line, using W to quickly mark a word, etc. This section of the documentation for advanced operations in c/p mode is described in more detail.

In general, you can move the cursor to the specified position, press the space to set a start tag, then move the cursor to the end position, press the space to set the second marker, and store the portion between the two markers in Copy/paste buffer and exit Copy/paste mode. In normal mode, you can use the shortcut key C-a] to paste the contents 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 the default level two profile of screen/ETC/SCREENRC and $HOME/.SCREENRC, such as setting screen options, customizing the binding key, setting the screens session self-launch window, enabling multiuser mode, customizing user access control, and more. You can also specify the screen configuration file yourself if you wish.

In the case of multi-user functions, screen is run in single-user mode by default, and you need to specify multiuser on in the configuration file to open multiuser mode via acl* (Acladd,acldel,aclchg ... ) command, you can flexibly configure other users to access your screen session. Please refer to Screen's man page for more information on the configuration file.

Screen session Command Linux

Related Article

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.