Continue executing the program after SSH or telnet is disconnected.

Source: Internet
Author: User
Tags exit in

Most content from: http://blog.zhangjianfeng.com/article/718

 

Do you often need SSH or Telent to remotely log on to Linux?
Server? Do you often have a headache for long-running tasks, such as system backup, ftp
Transmission and so on. Generally, we open a remote terminal window for each such task because it takes too long to execute. You must wait for the execution to complete. During this period, you cannot close the window or disconnect the connection.
Otherwise, the task will be killed.


Culprit: sighup Signal

Let's see why closing the window or disconnecting will cause the running program to die.

Linux/Unix has the following concepts:

  • Process Group: A collection of one or more processes. Each process group has a unique process group ID, that is, the ID of the Process leader process.
  • Session: a set of one or more process groups. It has a unique session leader ). The ID of the first process.
  • A single control terminal (controlling terminal) is available during the session ). The first process connecting to the control terminal is called the control process ). The process currently interacts with the terminal is called the foreground process group. Other process groups are called background process groups.

According to posix.1:

  • The default action of sighup is to terminate the program.
  • When the terminal interface detects that the network connection is disconnected, it sends the hanging signal to the control process (the first process in the session ).
  • If the first process of the session ends, the signal is sent to the foreground Process Group of the session.
  • When a process exits and an orphan process group is generated, if any process in the orphan process group is in the Stop state, the sighup and sigcont signals are sent to all processes in the process group.

Therefore, when the network is disconnected or the terminal window is closed, the control process receives the sighup signal and exits, causing other processes to exit during the session period.

Let's look at an example. Open two SSH terminal windows and run one of the top commands.

[root@tivf09 root]# top

In another terminal window, find the top process ID is 5180, and its parent process ID is 5128, that is, log on to shell.

[root@tivf09 root]# ps -ef|grep top root      5180  5128  0 01:03 pts/0    00:00:02 top root      5857  3672  0 01:12 pts/2    00:00:00 grep top

Use the pstree command to see the relationship more clearly:

[root@tivf09 root]# pstree -H 5180|grep top |-sshd-+-sshd---bash---top

Run the PS-XJ command. You can see that the login shell (PID 5128) and top are in the same session period. shell is the first process in the session period and the pgid of the Process Group is 5128, the pgid of the top process group is 5180, which is the foreground process group.

[root@tivf09 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. In the other window, you can see that the top is also killed.

[Root @ tivf09 root] # ps-Ef | grep 5128 root 18699 3672 0 00:00:00 pts/2 grep 5128







----------------------------------------------------------------------------



In Unix/Linux, you generally want to run a program in the background. Many of them use & at the end of the program to run the program automatically. For example, we want to run MySQL


In the background

/Usr/local/MySQL/bin/mysqld_safe-user = MySQL &

However, many of our programs do not support daemon like mysqld.
, Maybe our program is just a common program. Generally, even if the end of this program is used, if the terminal is closed, the program will be closed. To be able to run in the background, we can ignore the sighup signal. closing the window will not affect the running of the program. The nohup command can achieve this purpose, if the program's standard output/standard error is final
By default, nohup redirects it to the nohup. Out file. It is worth noting that the nohup command only causes the program to ignore the sighup signal and also requires the flag&
Run it in the background. We need to use the nohup command. For example, we have a start. sh needs to run in the background, and you want to continue running in the background, then use nohup: syntax format: nohup <command> [argument...] &

Such as nohup/root/start. Sh

After you press enter in shell, the following message is displayed:

[~] $ Appending output to nohup. Out

The standard output of the original program is automatically redirected to the nohup. Out file in the current directory, which plays the role of log.

But sometimes there is a problem at this step. When the terminal is closed, the process will be automatically closed. Check nohup. Out to see that the service will be automatically closed when the terminal is closed.

After consulting with the red-flag Linux engineer, he could not solve the problem. After executing the command on my terminal, the process he started was still running after the terminal was closed.

During the second demonstration to me, I found that I had a different detail from the one I used when operating the terminal: he needed to press any key on the terminal to return to the terminal after the shell prompts that nohup is successful.
Shell enters the command window and then enters exit in shell to exit the terminal. I click Close program to close the terminal every time the nohup is executed successfully .. So at this time
The session corresponding to this command is disconnected, so that the process corresponding to nohup is notified and shutdown is required together.

I did not notice the details, so I recorded them here.

Appendix: nohup command reference

Nohup command

Purpose: run the command without hanging up.

Syntax: nohup Command [Arg... ] [&]

Description: The nohup command is run by the command parameter and any related Arg.
The command specified by the parameter ignores all sighup signals. After logging out, run the nohup command to run the program in the background. To run the nohup command in the background, add
& (Represents the "and" symbol) to the end of the command.

Whether or not the output of the nohup command is redirected to the terminal, the output will be appended to the nohup. Out file in the current directory. If
The nohup. Out file cannot be written, and the output is redirected to the $ home/nohup. Out file. If no file can be created or opened for append, the command
The command specified by the parameter cannot be called. If a standard error is a terminal, write the specified command to all outputs of the standard error as the standard output and redirect it to the same file descriptor.

Exit status: This command returns the following export values:

126 you can find but cannot call the command specified by the command parameter.

127 The nohup command has an error or cannot find the command specified by the command parameter.

Otherwise, the exit status of the nohup command is the exit status of the command specified by the command parameter.

Nohup command and output file

Nohup command: If you are running a process and you think the process will not end when you exit the account, you can use the nohup command. This command can continue running the corresponding process after you exit the account/Close the terminal. Nohup means not to suspend (N ohang up ).

The command is generally in the form of nohup command &

Use the nohup command to submit a job

If you use the nohup command to submit a job, all the output of the job is redirected to a file named nohup. Out by default, unless the output file is specified separately:

Nohup command> myout. File 2> & 1 &

In the preceding example, the output is redirected to the myout. File file.

Use jobs to view tasks.

Disable using FG % N.

There are two other commonly used FTP
Tools ncftpget and ncftpput can be used for FTP uploading and downloading in the background, so that you can use these commands to upload and download files in the background.





----------------------------------------------------------------------------
 

Although nohup is easy to use, it is still relatively "simple". It can handle simple commands and is troublesome for complicated tasks that require human-computer interaction.

In fact, we can use a more powerful utility screen. Popular Linux distributions (such as Red Hat Enterprise Linux 4) usually come with screen utilities. If not, you can download them from the GNU screen official website.

[root@tivf06 ~]# rpm -qa|grep screen xscreensaver-4.18-5.rhel4.11 screen-4.0.2-5


Start using screen

To put it simply, screen is a window manager that can reuse a physical terminal among multiple processes. There is a session concept in screen. You can use a screen session
Create multiple screen Windows in each screen window, just like operating a real Telnet/SSH connection window. Create a new window in screen.
Methods:

1. Directly type the screen command in the command line.

[root@tivf06 ~]# screen

Screen creates a full screen window for executing shell. You can execute any shell program, as in the SSH window. In this window, type exit to exit the window. If this is the only window of the screen Session, the screen session will exit; otherwise, the screen will automatically switch to the previous window.

2. The screen command is followed by the program you want to execute.

[root@tivf06 ~]# screen vi test.c

Screen creates a single window session for executing VI test. C. Exit VI and exit the window/session.

3. Create a screen session in both of the preceding methods. We can also create a new window in an existing screen session. In the current screen window, TypeC-a c
, That is, press Ctrl + A, and then press C. Screen generates a new window in the session and switches to the window.

Screen also has more advanced features. 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/ABC file:

[root@tivf06 ~]# screen vi /tmp/abc

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
(You can directly disconnect the connection.) screen will display the detached prompt:

Temporarily interrupt session

After half an hour, I came back and found the screen session:

[root@tivf06 ~]# screen -ls There is a screen on:         16582.pts-1.tivf06      (Detached) 1 Socket in /tmp/screens/S-root.

Reconnect session:

[root@tivf06 ~]# screen -r 16582

Let's see what happened. It's great. Everything is there. Continue.

You may notice that a special key combination C-A is used to send commands to screen. This is because the information we typed on the keyboard is directly sent to the current screen window, and other methods must be used.
Send a command to the screen window manager. By default, screen receives a command starting with C-. In screen, this command is called Key Binding (Key
Binding), C-A is called the command character ).

You can useC-a ?
To view all key bindings. common key bindings include:

C-? Show all key binding information
C-A W Show list of all windows
C-a c- Switch to the previously displayed window
C-A C Create a new window for running shell and switch to it
C-A N Switch to the next window
C-A P Switch to the previous window (opposite to C-a n)
C-A 0 .. 9 Switch to window 0 .. 9
C- Send C-A to the current window
C-a d Temporarily disconnect screen session
C-A K Kill current window
C-[ Enter copy/rollback Mode


Screen common options

Use the key to bind C-? The default command key is c-a, and the Escape Character C-A (literal ^ A) is:

Screen common options

Because screen regards C-A as the beginning of the screen command, if you want the screen window to receive the C-a character, enter C-. Screen also allows you to use the-E Option to set your own command characters and escape characters. The format is:

-Exy X is the command character and Y is the character of the escape command character

The screen session started by the following command specifies that the command character is c-t, and the escape c-t character is T, through c-t? Command to see the change.

Custom command characters and escape characters

[root@tivf18 root]# screen -e^tt

Other common command options include:

-C file Use the configuration file instead of the default $ home/. screenrc
-D |-d [pid. TTY. Host] Instead of starting a new screen session, you can disconnect other running screen sessions.
-H num Specify the buffer size for historical rollback as num rows
-List |-ls List existing screen sessions in the format of PID. TTY. Host
-D-m Start a session in disconnected Mode
-R sessionowner/[pid. TTY. Host] Reconnect to a disconnected session. To connect to screen sessions of other users in multi-user mode, you must specify the sessionowner and set the setuid-root permission.
-S sessionname Specify a name for the session when creating a screen session
-V Display screen version information
-Wipe [Match] Same-list, but delete sessions that cannot be connected

In the following example, two screen sessions in the detached state are displayed. You can use screen-r <screen_pid> to reconnect to the screen:

[root@tivf18 root]# screen -ls There are screens on:         8736.pts-1.tivf18       (Detached)         8462.pts-0.tivf18       (Detached) 2 Sockets in /root/.screen.  [root@tivf18 root]# screen -r 8736

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:

[root@tivf18 root]# kill -9 8462 [root@tivf18 root]# screen -ls   There are screens on:         8736.pts-1.tivf18       (Detached)         8462.pts-0.tivf18       (Dead ???) Remove dead screens with 'screen -wipe'. 2 Sockets in /root/.screen.  [root@tivf18 root]# screen -wipe There are screens on:         8736.pts-1.tivf18       (Detached)         8462.pts-0.tivf18       (Removed) 1 socket wiped out. 1 Socket in /root/.screen.  [root@tivf18 root]# screen -ls   There is a screen on:         8736.pts-1.tivf18       (Detached) 1 Socket in /root/.screen.  [root@tivf18 root]#

The-D-M option is an interesting partner. They start a session in disconnected mode. You can connect to the session as needed later. Sometimes this is a very useful function. For example, we can use it to debug background programs. This option is more commonly used:-DMs sessionname

Start an initial disconnected screen session:

[root@tivf06 tianq]# screen -dmS mygdb gdb execlp_test

Connect to this session:

[root@tivf06 tianq]# screen -r mygdb


Manage your remote sessions

Let's take a look at how to use screen to solve the sighup problem. For example, we want to transfer a large file over FTP. If you log on to the system through SSH in the old way, run the FTP command to start
After transmission .. If the network speed is okay, congratulations, you don't have to wait too long. If the network is not good, wait honestly. You can only disconnect the SSH connection after the transmission is complete. Let's use screen
To try.

Log on to the system through SSH and Type Screen in the command line.

[root@tivf18 root]# screen

In the screen shell window, enter the FTP command, log on, and start transmission. Don't you want to wait? OK. In the window, type C-A D:

Manage your remote sessions

Then .. Log out of SSH? As you do, just don't kill the screen session.

Is it convenient? Furthermore, we can use screen to manage your remote sessions and save all your work content. Do you need to open many windows every time you log on to the system?
And then open and close these windows every day? Let screen help you "save". You only need to open an SSH window, create the screen window, and Exit C-
D "save" your work. After the next login, you can directly screen-r <screen_pid>.

It is better to give a name for each window so that you can remember it. Use C-A to name the window. Use C-a w to view the names of these windows. The names may appear in different positions. Use Putty:

Putty

Use telnet:

Telnet


More screen Functions

Screen provides rich and powerful customization functions. You can specify the default two-level configuration file/etc/screenrc and $ home/. screenrc in screen.
Set more options, such as setting screen options, customizing the binding key, setting the screen session auto-start window, enabling multi-user mode, and customizing user access control. If you want to, you can also
The screen configuration file has been specified.

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, you can use the ACL * (acladd, acldel, aclchg...) command to flexibly configure other users to access your screen session. More configuration files
For more information, see the man page of screen.

 

-------------------------------------

Problems encountered in practical application

 

[Root @ Dev ~] # Screen-ls
There is a screen on:
4083. down_video (detached)
1 socket in/var/run/screen/S-root.

The status shows that the screen session down_video has been disconnected (detach)

In this case, we can use screen-R to retrieve the image,

Description:-R: Retrieves a disconnected screen session.

Example: [root @ Dev ~] # Screen-r down_video
We can see that our program is still running and has not stopped in the middle, and now we can control it again.

So how to interrupt a screen session?
Example:

[Root @ Dev ~] # Screen-ls
There is a screen on:
4083. down_video (attached)
1 socket in/var/run/screen/S-root.

Note: The session: down_video is being connected,
In this case, I want to use this session

[Root @ Dev ~] # Screen-r down_video
There is a screen on:
4083. down_video (attached)
There is no screen to be resumed matching down_video.

In this case, the screen session cannot be obtained using-R.
In this case, you can only use the-D parameter to forcibly interrupt it.
[Root @ Dev ~] # Screen-D down_video
[4083. down_video detached.]

[Root @ Dev ~] # Screen-r down_video
Use-R to retrieve the screen.

Note: The original disconnected connection will display:
[Root @ Dev ~] # Screen-r down_video
[Remote detached]

 

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.