Programs that run after Linux shuts down the terminal (or SSH, etc.) automatically stop

Source: Internet
Author: User
Tags session id

Problem Description:

Before a Python service on the server, put it in the background to run.  Python pyserver.py &. When I close this SSH, the service is not available, log in to the server again, there is no such Python process.

Problem Locator:

By the appearance of the above problem, we can find that it is related to SSH shutdown. Why SSH is turned off will cause the running program to die. By looking at the relevant data, we find that the real culprit is the sighup signal.

In Linux, there are several concepts:

Process group: A collection of one or more processes, each with a unique process group ID, which is a process group
Session: A collection of one or more process groups, with a unique session-period-first process (session leader). The ID of the session ID that is the lead process.
Control process: The session first process connected to the control terminal is called the control process.
The process that is currently interacting with the terminal is called the foreground process group. The remaining process groups are called background process groups.

General Abbreviations:

  • PID = Process ID (generated by the kernel based on deferred reuse algorithm)
  • PPID = parent Process ID (can only be modified by the kernel)
  • Pgid = Process Group ID (child process, parent process can be modified)
  • SID = Session ID (the process itself can be modified, but with limitations, see below)
  • Tpgid= Control terminal process group ID (modified by control terminal to indicate the current foreground process group)

Relationship of session and process group:

Each time a user logs on to a terminal, a session is generated. Processes executed at that terminal during the period of time from user logon to user exit are part of this session.

Each process, in addition to a process ID, belongs to a process group. A process group is a collection of one or more processes, and each process group has a unique process group ID. Cases where multiple processes belong to a process group are multiple processes with pipelines "|" Number of connections to execute. If a single process is executed at the command line, this process group is the only one.

Hang-up signal (SIGHUP) The default action is to terminate the program.

When the terminal interface detects a disconnected network connection, the hang-up signal is sent to the control process (session-first process). If the session first process terminates, the signal is sent to the foreground process group for that session period. A process exit causes an orphan process group to occur if any of the orphaned process group processes are in the stop state, sending Sighup and sigcont signals to all processes in the process group.

Therefore, when the network is disconnected or the terminal window is closed, that is, after SSH disconnects, the control process receives a SIGHUP signal exit, which causes the other process to exit during the session.

The solution

Nohup command:

If you are running a process and you feel that the process will not end when you exit the account, you can use the Nohup command. This command can continue to run the process after you exit the account/close the terminal. Nohup is the meaning of not hanging up.

We are now starting to start the service Python pyserver.py and want to run in the background. We can use the Nohup command as follows:

Nohup python pyserver.py &

At this time the default program run output information to the current folder in the Nohup.out file, add & does not affect this command. Just let the program foreground or background run only.

Nohup Command Description:
Purpose: To run the command without hanging off.
Syntax: Nohup Command [Arg ...] [&]
Description: The nohup command runs commands specified by the command parameter and any related ARG parameters, ignoring all hang-up (SIGHUP) signals. Use the Nohup command to run a program in the background after logging off. To run the Nohup command in the background, add & (the symbol representing "and") to the end of the command.

The output is appended to the Nohup.out file in the current directory, regardless of whether the output of the Nohup command is redirected to the terminal. If the nohup.out file for the current directory is not writable, the output is redirected to the $HOME/nohup.out file. If no file can be created or opened for appending, then the command specified by the commands parameter is not callable. If the standard error is a terminal, then all output of the specified command to the standard error is redirected to the same file descriptor as the standard output.


Exit Status: The command returns the following exit values:

  • 126 you can find but not invoke commands specified by the command parameter.
  • The 127 nohup command has an error or cannot find the command specified by the commands parameter.

Otherwise, the exit status of the Nohup command is the command parameter that specifies the exit state of the commands.

Use jobs to view tasks and use FG%n to close.

Extended:

Why does the daemon, even if SSH is turned on, not affect its operation even if SSH is turned off?

Because their program is special, such as Httpd–k start running this, he does not belong to the SSHD process group, but a separate process group, so even if the ssh closed, and he does not have any relationship!

[[email protected] ~]# Pstree |grep http
|-httpd
[Email protected] ~]# pstree |grep Top
|-sshd-+-sshd-bash-top

The daemon's start command itself is special, unlike general commands, such as Mysqld_safe, which once used the daemon to run. So it is impossible to transform the general program into a daemon.

Programs that run after Linux shuts down the terminal (or SSH, etc.) automatically stop

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.