A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
Original address: http://www.ibm.com/developerworks/cn/linux/l-cn-screen/
Do you often need SSH or telent telnet to a Linux server? Do you often have headaches for long-running tasks 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 it to finish, during this period can not shut down the window or disconnect, otherwise this task will be killed, all halfway.Culprit: SIGHUP Signal
Let's see why shutting down the window/disconnecting will cause the running program to die.
In Linux/unix, there are several concepts:
According to POSIX.1 definition:
Therefore, when the network is disconnected or the terminal window is closed, the control process receives a SIGHUP signal exit, which causes the other process to exit during the session.
Let's look at an example. Open the two SSH terminal window and run the top command in one of them.
[email protected] root]# Top
In another terminal window, find the process ID of top 5180 and its parent process ID is 5128, which is the login shell.
[Email protected] root]# ps-ef|grep toproot 5180 5128 0 01:03 pts/0 00:00:02 toproot 5857 3672 0 01:12 pts/2 00:00:00 grep top
Using the Pstree command, you can see this relationship more clearly:
[Email protected] root]# pstree-h 5180|grep top|-sshd-+-sshd---bash---top
Using the PS-XJ command, you can see that the login shell (PID 5128) and top are in the same session period, the shell is the session first process, the process group Pgid is the 5128,top process group Pgid is 5180, is the foreground process group.
[Email protected] root]# ps-xj|grep 5128 5126 5128 5128 5128 pts/0 5180 S 0 0:00-bash 5128 5180 5180 5128 pts/0 5180 s 0 0:50 top 3672 18095 18094 3672 pts/2 18094 s 0 0:00 grep 5128
Close the first SSH window, and in the other window you can see that top is also killed.
[[email protected] root]# ps-ef|grep 5128root 18699 3672 0 04:35 pts/2 00:00:00 grep 5128
If we can ignore the sighup signal, closing the window should not affect the operation of the program. The Nohup command can do this, and if the standard output/standard error of the program is the terminal, Nohup redirects it to the Nohup.out file by default. It is important to note that the Nohup command only causes the program to ignore the sighup signal, and it needs to use tags & put it in the background to run.
Nohup <command> [argument ...] &
Although Nohup is easy to use, it is still relatively "crude", for simple commands can be dealt with, for the complex needs of human-computer interaction task is troublesome.
In fact, we can use a more powerful utility screen. A popular Linux distribution, such as Red Hat Enterprise Linux 4, usually comes with the screen utility, and if not, it can be downloaded from the official website of the GNU screen.
Start using screen[Email protected] ~]# rpm-qa|grep screenxscreensaver-4.18-5.rhel4.11screen-4.0.2-5
Simply put, screen is a window manager that can multiplex a physical terminal across multiple processes. Screen has the concept of session, users can create multiple screen windows in a screen session, in each of the screens window like the operation of a real telnet/ssh connection window. There are several ways to create a new window in screen:
1. Type the screen command directly at the command line
[Email protected] ~]# screen
Screens will create a full-screen window that executes the shell. You can execute any shell program, just like in the SSH window. Type exit in the window to exit the window, if this is the only window of the screen session, the screen session exits, otherwise screen automatically switches to the previous window.
2. Screen command followed by the program you are going to execute.
[Email protected] ~]# Screen VI test.c
Screen creates a single-window session that executes VI test.c, and exiting VI exits the window/session.
3. Both of these methods create a new screen session. We can also create a new window in an existing screen session. In the current screen window
C-a c , type the CTRL key +a, and then press the C key, screen to generate a new window within the session and switch to that window.
Screen also has more advanced features. 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/abc file:
[Email protected] ~]# screen VI/TMP/ABC
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:
Came back in half an hour and found the screen session:
[[email protected] ~]# Screen-lsthere is a screens on: 16582.pts-1.tivf06 (Detached) 1 Socket In/tmp/screens/s-ro Ot.
[Email protected] ~]# screen-r 16582
See what's going on, it's great, it's all there. Keep doing it.
You may notice that a special key combination c-a is used to send a command to screen. This is because the information we type on the keyboard is sent directly to the current screen window and must be issued in a different way to the screen window manager, which, by default, receives a command starting with C-a. This form of command is called key binding in screen, C-a is called command character.
C-a ? view all the key bindings by using the key bindings that are commonly used:
|C-a?||Show all key binding information|
|C-a W||Show list of all windows|
|C-a c-a||Switch to the previously displayed window|
|C-a C||Create a new window to run the shell and switch to that window|
|C-a N||Switch to the next window|
|C-a P||Switch to the previous window (relative to C-a N)|
|C-a 0..9||Switch to Window 0: 9|
|C-a A||Send c-a to current window|
|C-a D||Temporarily disconnecting a screen session|
|C-a K||Kill the current window|
|C-a [||Enter copy/rollback mode|
Using the key binding c-a? command, you can see that the default command character is C-a, and the character of the escaped c-a (literal ^a) is a:Screen common options
Because screen sees C-a as the start of the screens command, enter c-a A If you want the c-a character to be received by the window. Screen also allows you to use the-e option to set your own command and escape characters in the following format:
-exy x is the command character, and y is the character that escapes the command character
The following command launches a screen session that specifies the command character C-t, the character of the escaped c-t is T, and the change is visible through the C-T command.
Custom command characters and escape characters[Email protected] root]# SCREEN-E^TT
Other common command options are:
|-C file||Using the profile file instead of using the default $HOME/.SCREENRC|
|-d|-d [Pid.tty.host]||Instead of opening a new screen session, you are disconnecting other running screen sessions|
|-H num||Specifies that the history rollback buffer size is num rows|
|-list|-ls||List existing screen sessions in pid.tty.host format|
|-d-m||Start a session that starts in disconnected mode|
|-R sessionowner/[Pid.tty.host]||Reconnect a disconnected session. Connect to other users in multi-user mode screen session needs to specify Sessionowner, requires Setuid-root permissions|
|-S SessionName||Assigning a name to a session when creating a screen session|
|-V||Show screen version information|
|-wipe [Match]||Same-list, but delete disconnected sessions|
The following example shows that there are currently two screen sessions in the detached state, and you can use Screen-r <screen_pid> reconnect:
[Email protected] root]# Screen–lsthere is screens on: 8736.pts-1.tivf18 (Detached) 8462.pts-0.tivf18 (Detached) 2 Sockets in/root/.screen. [Email protected] root]# screen–r 8736
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:
[Email protected] root]# kill-9 8462[[email protected] root]# Screen-ls There is screens on: 8736.pts-1.tivf18 (Detached) 8462.pts-0.tivf18 (Dead???) Remove dead screens with ' screen-wipe '. 2 Sockets In/root/.screen. [Email protected] root]# Screen-wipethere is screens on: 8736.pts-1.tivf18 (Detached) 8462. Pts-0.tivf18 (removed) 1 socket wiped Out.1 socket in/root/.screen.[ [Email protected] root]# Screen-ls There is a screens on: 8736.pts-1.tivf18 (Detached) 1 Socket In/root/.sc Reen. [Email protected] root]#
The-D–M option is an interesting pair of partners. They start a session that starts in disconnected mode. You can connect to the session when you need it later. Sometimes this is a useful feature, such as we can use it to debug background programs. A more common pairing of this option is:-dms sessionname
Start a screen session that is disconnected from the initial state:
[Email protected] tianq]# Screen-dms mygdb gdb execlp_test
To connect to the session:
Manage your remote sessions[Email protected] tianq]# screen-r mygdb
Let's take a look at how to use screen to solve sighup problems, such as now we want FTP to transfer a large file. If you follow the old approach, SSH logs into the system, the direct FTP command begins to transfer, and then: If the network speed can also, congratulate you, do not have to wait too long, if the network is not good, honestly wait, can only transfer and then disconnect SSH connection. Let's use screen to try.
SSH login to the system and type screen at the command line.
[Email protected] root]# screen
Enter the FTP command in the Screen Shell window, log in, and start the transfer. Don't want to wait? OK, type c-a D in the window:Manage your remote sessions
And then.. Exit SSH login? Whatever you want, just don't kill the screen session.
Isn't it convenient? Further, we can actually use screen to manage your remote session and save all your work. Do you have to open a lot of Windows every time you log on to the system, and then open the Windows repeatedly every day? Let screen to help you "save" it, you just need to open an SSH window, create the required screen window, exit c-a D "Save" your work, the next time you log in directly screen-r <screen_pid> will be OK.
It's best to give each window a name, so it's good to remember. Use C-a A to name the window. These window names can be seen using C-a W, where the names may appear differently. Using putty:Putty
Using Telnet:Telnet More screen Features
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.Resources
Linux tips: Use screen to manage your remote sessions
Start building with 50+ products and up to 12 months usage for Elastic Compute Service