Linux often uses putty or securecrt to telnet to the server via SSH, but if the remote terminal window is closed during the execution of the program, the previously running program is immediately killed. This is bad for some programs that take a long time, and this article describes how to use the screen command to solve this problem and other features that screen provides.
About screen
When using SSH to telnet to the server, if you accidentally shut down the remote terminal window during the process of running the program, or if the network is disconnected or the computer freezes, the connection session of the host and the server is lost immediately, the program that is just running or the work is lost at the same time, this is a very frustrating thing. Using screen can be a good solution to this problem.
Screen is a GNU program developed for the command line terminal switch free software, to achieve full-page window management functions, can mix multiple work to a terminal. In general Linux systems with the screen command, if not, the Ubuntu class system can be easily installed by the following command:
sudo apt-get install screen
The following commands are available in the Red Hat class system:
Yum Install screen
Below is a brief introduction to several common features of screen.
Multi-session
With a remote server such as Putty, create a new screen session by running the following command directly from the command line:
Screen
You can also specify the name of the session, and the following is the creation of a session with the name Noalgo:
Screen-s Noalgo
You can also specify the program to run when you create a new session, and the VI editor is run, and notice that exiting the VI editor means exiting the screen session.
Screen-s Noalgo VI HELLOWORLD.C
After the new session is entered into the screen of the world, what is done here and in the ordinary shell things no difference, but the session can be resumed at this time, even if a network outage, you can run the screen command again to return to the session, and, When you return, the screen is displayed, and if the program is running dynamically, the latest results are displayed. If something needs to go away, and the program on the server needs to be running at the same time, you can detach the session from command D. The actions in the screen session start with Ctrl + A, so you need to press CTRL + A and then press D, which is said:
C-a, D
At this point will go back to the original Putty window, you can switch off putty to do other things. When you want to go back, you can log in first through Putty, and then run the following command to view the screen sessions that are already in the system:
Screen-ls
Or
Screen-list
The results are similar to the following:
[Email protected]:~$ Screen-lsthere is a screens on: 15057.noalgo (01/30/2015 06:16:45 AM) (Detached) 1 Socket In/var/run/screen/s-noalgo.
You can then go back to the screen session with the following command:
Screen-r 15075
Or go back directly through the session name:
Screen-r Noalgo
You can continue to do things just before you leave. In addition, session sharing can be achieved through the following commands, when multiple users log in to the same session, and if they are in the same window at the same time, each other's actions are synchronized to each user, that is, the effect of sharing the desktop.
Screen-x
Multi-window
In a normal shell environment, if you want to execute multiple programs at the same time, you can do this by alternating CTRL + Z, as well as the FG and BG commands, but screen provides a multi-window capability to do the same. To create a new session with the screen command:
Screen-s Noalgo
A window with the default screen session is entered and a new window can be created with the following command:
C-a, C
Switching between different windows can be done with the following two commands, respectively, into the next and previous windows:
C-a, Nc-a, p
Use the following command to see the current number of Windows, marked with the * number of the currently located window:
C-a, W
Forcibly closes a window with the following command, terminating the current session if only the last window is currently available:
C-a, K
The following command can also achieve the same effect:
Exit
When using multiple windows, you can increase efficiency by dividing the screen into several areas. Use the following command to split the screen, split horizontally and vertically, respectively:
C-a, sc-a, |
When you have multiple screens, use the following command to switch:
C-a, Tab
Use the following command to turn off a split screen,
C-a, X
or close all other areas of the current zone:
C-a, Q
Screen detail Parameters
The above is a simple example of the common use of screen, the following parameters are described in detail. The command syntax for screen is:
screen [-amrvx-ls-wipe][-d <name>][-h <line>][-r <name][-s][-s <name>]
The parameters have the following meanings:
- -A: Adjust all windows to the current size of the terminal.
- -D: detaches the specified screen session.
- -H: Specifies the number of buffer rows in the window.
- -M: Forces a new screen session to be established even if a screen session is currently in session.
- -R: Resumes the detached screen session.
- -R: Try to restore the offline session first. If an offline session is not found, a new screen session is established.
- -S: Specifies the shell to execute when a new window is created.
- -S: Specifies the name of the screen session.
- -V: Displays version information.
- -X: Restore the screen session that was offline before.
- -ls: Displays all current screen sessions.
- -list: Displays all current screen sessions.
- -wipe: Check all current screen sessions and delete screen sessions that are not already available.
The commands you can use in each screen session are as follows. Note that screen commands are all started with Ctrl + A (C-A), with the following omitted c-a instead of the next key:
- ?: Help, display the key binding situation.
- C:create, create a new window.
- N:next, switch to the next window.
- p:previous, switch to the previous window.
- M: View active status.
- X: Lock the current window and unlock it with the user's password.
- D:detach, the current session is temporarily left, and can then be resumed.
- Z: Place the current session in the background and use the Shell's FG command to go back.
- W:windows, lists the windows that have been created.
- T:time, displays the current time.
- K:kill, forcibly closes the current window.
- [0..9]: Switch to 0..9 window.
- [Space]: switch from window 0 to window 9.
- C-A: Switch between the two most recently used Windows.
- S: Horizontal split screen.
- |: Vertical Split screen.
- X: Closes the current split screen.
- Q: Turn off all sub-screens except the current split screen.
- [Tab]: Toggle in Split screen.
- [: Copy, enter the copy mode, this time can be rolled back, search, copy, as with the use of VI.
- ]:paste, paste the content that you just selected in copy mode.
Commands that can be used in copy mode include
- C-b:backward,pageup.
- C-f:forward,pagedown.
- H: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:forward one word, move forward a word.
- B:backward one word, a word later.
- Space: The first time by marking the starting point, the second time by marking the end.
- ESC: End copy mode.
Not all of the parameters listed here, need more detailed content, can be obtained directly from the following command:
Mans screen
Here is the c-a, what is displayed, can prompt each key to bind the function, also very convenient.
Screen key bindings, page 1 of 1. Command key: ^a Literal ^a:a break ^b B History {} Other ^a split S Clear C Info i pow_break B suspend ^z Z Colon: Kill K K Pow_detach D time ^t T copy ^[[lastmsg ^m M Prev ^h ^p P ^? Title A detach ^d D license, quit \ Vbell ^g DIGR APh ^v lockscreen ^x X readbuf < version V Displays * log H redisplay ^l L width W dumptermcap. Login L Remove X windows ^w W fit F meta a REM Ovebuf = Wrap ^r R flow ^f F Monitor M Reset Z writebuf > Focus ^i next ^@ ^n sp N screen ^c C Xoff ^s S hardcopy h number N select ' Xon ^q Q Help? Only Q Silence _ ^] paste. " windowlist-b-select-0 Select 67 Select-Select-Select-Select- Select OnO Select 9I Login login off] paste | Split-v:kb:focus prev
- This article is from: Linux Tutorial Network
Use of the Linux screen