Linked Questions

Popular Questions

Python's daemon PPID not equal to 1

Asked by At

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.

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


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):

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:

[email protected]:/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.

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


Related questions:

Related Questions