[systemd-devel] How to run forking daemon with systemd in correct way, without errors?

Lennart Poettering lennart at poettering.net
Thu Nov 23 14:44:03 UTC 2017


On Do, 23.11.17 00:02, Gena Makhomed (gmm at csdoc.com) wrote:

> On 22.11.2017 23:27, Reindl Harald wrote:
> 
> > > I am use nginx web server (http://nginx.org/) with systemd.
> > > But I see errors in /var/log/messages when running nginx:
> > > 
> > > systemd: Starting nginx - high performance web server...
> > > systemd: Failed to read PID from file /var/run/nginx.pid: Invalid
> > > argument
> > > systemd: Started nginx - high performance web server.
> > > 
> > > systemd: Starting nginx - high performance web server...
> > > systemd: PID file /var/run/nginx.pid not readable (yet?) after start.
> > > systemd: Started nginx - high performance web server
> 
> > either the daemon needs to be fixed
> 
> Ok, but can you tell, what is wrong with daemon?
> 
> this is nginx daemonization code:
> http://hg.nginx.org/nginx/file/tip/src/os/unix/ngx_daemon.c

This is not how UNIX daemonization is supposed to work. The daemon is
supposed to fork twice: the first child is supposed to call
setsid() and then call fork() and then exit(), and the grandchild is
then supposed to be the main daemon process, so that:

a) it's definitely reparented to PID 1

b) It runs within its own setsid session, that has no leader

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list