[systemd-devel] How to run forking daemon with systemd in correct way, without errors?
Lennart Poettering
lennart at poettering.net
Thu Nov 23 14:35:24 UTC 2017
On Mi, 22.11.17 22:37, Gena Makhomed (gmm at csdoc.com) wrote:
> But nginx is forking daemon - when start process exited -
> no warranty what pidfile already created by child process.
>
> This is bug in nginx code? And this bug should be fixed in nginx?
Yes, and that's not just broken in the context of systemd, but on SysV
services, too. Think "/etc/init.d/nginx start ; /etc/init.d/nginx
stop" — if the PID file is not written fully by the time nginx forked
and the parent exited, then such a stop immediately following will
fail too...
Daemons need to either write the PID file in the parent, after forking
off the daemon, and before exiting, or the daemon process can do that,
but then there needs to be some form of synchronization that makes
sure that the parent doesn't exit before the daemon process finished
writing the PID file.
This is documented in daemon(7) btw:
https://www.freedesktop.org/software/systemd/man/daemon.html
> But "daemon(); write_pidfile();" is common pattern
> used by many services and even in library functions.
Yeah, and it's too simplistic unfortunately.
> ExecStop=/bin/kill -s TERM $MAINPID
This line is redundant btw, systemd does that anyway.
Lennart
--
Lennart Poettering, Red Hat
More information about the systemd-devel
mailing list