Python的守护进程PPID不等于1

时间:2021-08-25 19:24:02

Creating daemons in Linux is quite complex issue, but it's very well documented in daemon(7) manual. Thankfully there is python-daemon module for Python 2 and 3 that implement PEP3143, so I'm using it.

在Linux中创建守护进程是一个非常复杂的问题,但它在守护进程(7)手册中有很好的记录。值得庆幸的是,Python 2和3的python-daemon模块实现了PEP3143,所以我正在使用它。

Here comes the question: when I was playing with python-daemon module I was surprised that daemon's PPID is not 1. Why?

问题就出现了:当我玩python-daemon模块时,我很惊讶守护进程的PPID不是1.为什么?


Simple example:

简单的例子:

import daemon
import time
import os

with open('/tmp/test.log', 'w') as logfile:
    c = daemon.DaemonContext(stdout=logfile)
    with c:
        print('In daemon...')
        for i in range(10):
            print('My PID is {}, PPID is {}'.format(os.getpid(), os.getppid()))
            time.sleep(2)

Content of test.log after 20 seconds from starting the above script (I recommend tail -f /tmp/test.log):

启动上述脚本20秒后test.log的内容(我建议使用tail -f /tmp/test.log):

In daemon...
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736
My PID is 31397, PPID is 1736

It turned out that process with PID 1736 is /lib/systemd/systemd:

事实证明,使用PID 1736的进程是/ lib / systemd / systemd:

patryk@mycomp:/tmp$ ps -fq 1736
UID        PID  PPID  C STIME TTY          TIME CMD
patryk    1736     1  0 kwi12 ?        00:00:00 /lib/systemd/systemd --user

Recently I was implementing daemons in C (on the same machine with systemd installed) and AFAIR all daemons had PPID = 1. All manuals I came across mention that daemon's PPID is always 1.

最近我在C中实现守护进程(在安装了systemd的同一台机器上)和AFAIR所有守护进程都有PPID = 1.我遇到的所有手册都提到守护进程的PPID始终为1。

Does systemd changed it? Does systemd process awaits for all processes – including daemons? Is it daemon's correct behavior?

systemd是否改变了它? systemd进程是否等待所有进程 - 包括守护进程?是守护进程的正确行为吗?


Related questions:

相关问题:

1 个解决方案

#1


3  

The short answer is: It doesn't matter.

简短的回答是:没关系。

The important thing is that all daemons have a parent process like init that will reap children when they die, by calling wait(). Otherwise the process will become a zombie when it exits.

重要的是,所有守护进程都有一个像init这样的父进程,通过调用wait()可以在子进程死亡时收获它们。否则,该过程将在退出时变为僵尸。

There's nothing particularly special about having a parent PID of 1. The manual page you linked to says this process is PID 1 for old, SysV-style daemons, but doesn't say so for new SystemD-style daemons. init always runs as PID 1, and traditionally it was the parent of daemons. But it doesn't need to be so.

将父PID设置为1并没有什么特别之处。您链接到的手册页说这个过程对于旧的SysV风格的守护进程是PID 1,但对于新的SystemD风格的守护进程没有这样说。 init始终作为PID 1运行,传统上它是守护进程的父进程。但它不需要如此。

systemd --user manages user services. Thus, it makes sense why this process becomes your daemon's parent process when you run it (as a user).

systemd --user管理用户服务。因此,当您运行它(作为用户)时,此进程成为您的守护进程的父进程是有道理的。

One must be careful when reading documentation about *nix, a platform that's been around for decades. Things change and manuals become obsolete, or can be interpreted in the wrong context. SystemD brings major change to a lot of things about the Linux platform.

在阅读关于* nix的文档时必须要小心,这个平台已经存在了几十年。事情发生变化,手册变得过时,或者可能在错误的背景下被解释。 SystemD为Linux平台带来了很多重大变化。

#1


3  

The short answer is: It doesn't matter.

简短的回答是:没关系。

The important thing is that all daemons have a parent process like init that will reap children when they die, by calling wait(). Otherwise the process will become a zombie when it exits.

重要的是,所有守护进程都有一个像init这样的父进程,通过调用wait()可以在子进程死亡时收获它们。否则,该过程将在退出时变为僵尸。

There's nothing particularly special about having a parent PID of 1. The manual page you linked to says this process is PID 1 for old, SysV-style daemons, but doesn't say so for new SystemD-style daemons. init always runs as PID 1, and traditionally it was the parent of daemons. But it doesn't need to be so.

将父PID设置为1并没有什么特别之处。您链接到的手册页说这个过程对于旧的SysV风格的守护进程是PID 1,但对于新的SystemD风格的守护进程没有这样说。 init始终作为PID 1运行,传统上它是守护进程的父进程。但它不需要如此。

systemd --user manages user services. Thus, it makes sense why this process becomes your daemon's parent process when you run it (as a user).

systemd --user管理用户服务。因此,当您运行它(作为用户)时,此进程成为您的守护进程的父进程是有道理的。

One must be careful when reading documentation about *nix, a platform that's been around for decades. Things change and manuals become obsolete, or can be interpreted in the wrong context. SystemD brings major change to a lot of things about the Linux platform.

在阅读关于* nix的文档时必须要小心,这个平台已经存在了几十年。事情发生变化,手册变得过时,或者可能在错误的背景下被解释。 SystemD为Linux平台带来了很多重大变化。