Screen command basic tutorial for Linux systems

Source: Internet
Author: User
Tags sessions switches terminates ssh scp command truncated

As a Linux server administrator, you often use SSH to log on to a remote Linux machine for some time-consuming operations.

Maybe you've encountered a remote login to Linux using telnet or SSH, running some programs. If these programs need to run for a long time (a few hours) and the program runs with a network failure or a client failure, the client's link to the remote server is terminated, and the command that the remote server does not end properly will be forced to terminate.

Again, for example, after you ssh to the host, start bulk of the SCP command, if the SSH thread is disconnected, the SCP process is interrupted. Some time-consuming jobs are running on the remote server, but the work is not finished, it will be off the operation, and how to be good?

We use the screen command to solve this problem very well. Implementation to continue executing the program on the server while disconnecting ssh.

So what's screen command?

Screen is called a Full-screen window manager, which makes it easy to get multiple virtual terminals on a physical terminal.

Screen Feature Description:

In simple terms, screen is a window manager that can multiplex a physical terminal across multiple processes, which means you can use a single terminal window to run multiple terminal applications. Screen has the concept of a session, where users can create multiple screen windows in a screen session, as in each screens window, as in the case of a real telnet/ssh connection window.

Screen command syntax:

screen [-amrvx-ls-wipe][-d < job name >][-h < line >][-r < job name >][-s][-s < job name]

Screen Command parameters:

-A-[r| R] All windows are adjusted to the current size of the terminal.

-c filename Replaces screen's configuration file '. SCREENRC ' with the specified filename file.

-d [Pid.tty.host] disconnects the screen process (when you use this command, screen's state must be attached, which means a user is connected to screen). The name of the general process is represented in the form of pid.tty.host (the state can be seen with the Screen-list command).

-d [Pid.tty.host] implements the same functionality as the-d command, except that if executed successfully, it will kick off the original user in screen and let him logout.

-H < number of rows > The number of buffer lines for the specified window.

-ls or –list displays all current screen jobs.

-M forces a new screen job to be created even if the screen job is currently in the job.

-p number or name to select a window in advance.

-R [Pid.tty.host] restores the offline screen process, and if there are multiple disconnected processes, you need to specify [Pid.tty.host]

-R attempts to recover an offline job first. If you cannot find an offline job, create a new screen job.

The-s shell specifies the shell to execute when creating a new window.

-S < job name > Specifies the name of the screen job. (used instead of [Pid.tty.host] 's naming method to simplify operations).

-V Displays version information.

-wipe checks all current screen jobs and deletes screen jobs that are no longer available.

-X Offline screen job before recovery.

General usage of the screen command:

Screen-d-R: Connect a screen process, and if the process is attached, kick off the remote user before connecting.

Screen-d-R: Connect a screen process, and if the process is attached, kick off the remote user and let him logout reconnect

Screen-ls or-list: Displays the existing screen process, common commands

SCREEN-M: If you are in a screen process, you can create a new window by using the shortcut key Crtl+a C, or by simply playing screens, screen-m you will be able to make a newly created one.

SCREEN-DM: Create a new screen, and the default is the detached mode, which is not connected up after the building.

Screen-p number or name: Select a window in advance.

Screen to implement the background of the simple steps to run the program:

1> to do something, first use the command to create a screen:

The code is as follows:

[linux@user~]$ screen-s Test1

2> can then operate inside, and if your task is not finished, leave screen with the command:

The code is as follows:

[linux@user~]$ ctrl+a+d #按Ctrl +a, then press D to keep screen

[Detached] #这时会显示出这个提示, the instructions have been preserved screen

If you are finished working, enter directly:

The code is as follows:

[Linux@user~]$ Exit #这样就表示成功退出了

[Screen is terminating]

3> If you left screen last time, you can use the command to view:

The code is as follows:

[Linux@user~]$ Screen-ls

There is a screens on:

9649.test1 (Detached)

Restore screen and use the command:

The code is as follows:

[linux@user~]$ screen-r test1 (or 9649)

Shortcut keys used in the screen command

CTRL + A C: Create a window

CTRL + A W: List of Windows

CTRL + A N: Next window

CTRL + A P: Previous window

CTRL + a 0-9: Toggle between window No. 0 and 9th windows

CTRL + A (uppercase): Closes the current window and switches to the next window (when the last window is exited, the terminal terminates automatically and returns to the original Shell state)

Exit: Closes the current window and switches to the next window (when the last window is exited, the terminal terminates automatically and returns to the original Shell state)

CTRL + A D: Exits the current terminal, returns the shell command state before loading screen

Multiple windows

screen, like many window managers, can support multiple windows. This feature is useful when working with multiple tasks and not opening new sessions at the same time. As a system administrator, I often have to open four or five SSH sessions simultaneously. Under each shell, I might have to deal with two or three tasks. If you don't use screen, you need 15 SSH sessions, 15 logons, 15 windows, and so on. With screen, each system is assigned to a separate session, and I manage the different jobs on the system through screens.

To open a new window, you only need to use "ctrl-a" "C". A new window is created that displays a default command prompt. For example, I can run the top command and then open a new window to do other work. Top Keep running! You can experiment, start screen and run top. (Note: I truncated multiple screens to save space.) )

Start Top

The code is as follows:

mem:506028k av, 500596K used, 5432K free,

0K shrd, 11752K Buff

swap:1020116k av, 53320K used, 966796K free

393660K Cached

< p> PID USER PRI NI SIZE RSS SHARE STAT%cpu%me

6538 Root 0 1892 1892 596 R 49.1 0.3

6614 Root 0 1544 1544 668 S 28.3 0.3

7198 Admin 0 1108 1104 828 R 5.6 0.2

You can now open a new window with "Ctrl-a" "C"

The code is as follows:

[Admin@ensim admin]$

To get back to top with use "Ctrl-a" n "

mem:506028k av, 500588K used, 5440K free,

0K shrd, 11960K Buff

swap:1020116k av, 53320K used, 966796K free

392220K Cached

< p> PID USER PRI NI SIZE RSS SHARE STAT%cpu%me

6538 Root 0 1892 1892 596 R 48.3 0.3

6614 Root 0 1544 1544 668 S 30.7 0.3

You can create multiple windows and then switch to the next window via "Ctrl-a" "n", or use "Ctrl-a" "P" to return to the previous window. When you work in other Windows, every program in the other window will remain running.

Exit screen

There are two ways to get out of screen. The first is like logging out a shell, you can terminate a window by "ctrl-a" "K" or "exit". So the current window will be closed, and if you open more than one window, you'll go directly to one of the rest, and if it's the only window, you're out of screen.

Another way to exit screen is to detach the window. This approach simply closes the window but the process is still running. If you have a process that is determined to execute for a long time and you need to close the SSH program, you can use "Ctrl-a" "D" to detach the window. This will bring you back to the shell. All screen windows stay there and you can take them back later. (Translator Note: This is very much like our actual minimized windows and programs running in the background)

Take over the session

If you're using screen for a long time compiling a program, suddenly your connection is disconnected. Do not worry, screen will save your compilation progress. After you log back in to your operating system, use the screen list tool to see which sessions are running:

The code is as follows:

[Root@gigan root]# Screen-ls

There are screens on:

31619.ttyp2.gigan (Detached)

4731.ttyp2.gigan (Detached)

2 Sockets in/tmp/screens/s-root.

Here, I have two different screen sessions. To take over one of these, use the Restore Window command:

The code is as follows:

[Root@gigan Root] #screen-R 31619.ttyp2.gigan

Just use the-r option to connect to the name of the session, and now you can go back to the screen again. Happily, you can take over again anywhere. You can use screen to start a job and then quit, both at the office and on other clients.

Multiple windows

screen, like many window managers, can support multiple windows. This feature is useful when working with multiple tasks and not opening new sessions at the same time. As a system administrator, I often have to open four or five SSH sessions simultaneously. Under each shell, I might have to deal with two or three tasks. If you don't use screen, you need 15 SSH sessions, 15 logons, 15 windows, and so on. With screen, each system is assigned to a separate session, and I manage the different jobs on the system through screens.

To open a new window, you only need to use "ctrl-a" "C". A new window is created that displays a default command prompt. For example, I can run the top command and then open a new window to do other work. Top Keep running! You can experiment, start screen and run top. (Note: I truncated multiple screens to save space.) )

Start Top

The code is as follows:

mem:506028k av, 500596K used, 5432K free,

0K shrd, 11752K Buff

swap:1020116k av, 53320K used, 966796K free

393660K Cached

< p> PID USER PRI NI SIZE RSS SHARE STAT%cpu%me

6538 Root 0 1892 1892 596 R 49.1 0.3

6614 Root 0 1544 1544 668 S 28.3 0.3

7198 Admin 0 1108 1104 828 R 5.6 0.2

You can now open a new window with "Ctrl-a" "C"

The code is as follows:

[Admin@ensim admin]$

To get back to top with use "Ctrl-a" n "

mem:506028k av, 500588K used, 5440K free,

0K shrd, 11960K Buff

swap:1020116k av, 53320K used, 966796K free

392220K Cached

< p> PID USER PRI NI SIZE RSS SHARE STAT%cpu%me

6538 Root 0 1892 1892 596 R 48.3 0.3

6614 Root 0 1544 1544 668 S 30.7 0.3

You can create multiple windows and then switch to the next window via "Ctrl-a" "n", or use "Ctrl-a" "P" to return to the previous window. When you work in other Windows, every program in the other window will remain running.

Exit screen

There are two ways to get out of screen. The first is like logging out a shell, you can terminate a window by "ctrl-a" "K" or "exit". So the current window will be closed, and if you open more than one window, you'll go directly to one of the rest, and if it's the only window, you're out of screen.

Another way to exit screen is to detach the window. This approach simply closes the window but the process is still running. If you have a process that is determined to execute for a long time and you need to close the SSH program, you can use "Ctrl-a" "D" to detach the window. This will bring you back to the shell. All screen windows stay there and you can take them back later. (Translator Note: This is very much like our actual minimized windows and programs running in the background)

Take over the session

If you're using screen for a long time compiling a program, suddenly your connection is disconnected. Do not worry, screen will save your compilation progress. After you log back in to your operating system, use the screen list tool to see which sessions are running:

The code is as follows:

[Root@gigan root]# Screen-ls

There are screens on:

31619.ttyp2.gigan (Detached)

4731.ttyp2.gigan (Detached)

2 Sockets in/tmp/screens/s-root.

Here, I have two different screen sessions. To take over one of these, use the Restore Window command:

The code is as follows:

[Root@gigan Root] #screen-R 31619.ttyp2.gigan

Just use the-r option to connect to the name of the session, and now you can go back to the screen again. Happily, you can take over again anywhere. You can use screen to start a job and then quit, both at the office and on other clients.

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.