Creating daemons on Linux is a pretty tricky question , but it is very well documented in daemon(7) manual. Fortunately, there is a python-daemonmodule for Python 2 and 3 that implement PEP3143 , so I use it.
The question arises: when I played with the module python-daemon, I was surprised that the PPID daemon is not 1 . Why?
A 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 test.log20 seconds after running 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 the process with 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
C ( systemd) AFAIR PPID = 1. , , , PPID 1.
systemd ? systemd awaits - ? ?
: