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

Gena Makhomed gmm at csdoc.com
Wed Nov 22 20:37:43 UTC 2017


Hello, All!

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.

As I can see from systemd sources - these errors come from function 
service_load_pid_file():
https://github.com/systemd/systemd/blob/master/src/core/service.c#L815

As I understand from comments in function service_enter_start():
https://github.com/systemd/systemd/blob/master/src/core/service.c#L1909

/* For forking services we wait until the start
  * process exited. */

And comments in function service_sigchld_event():
https://github.com/systemd/systemd/blob/master/src/core/service.c#L2959

/* Forking services may occasionally move to a new PID.
  * As long as they update the PID file before exiting the old
  * PID, they're fine. */

Systemd think what service is fully started after start process exited?

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?

But "daemon(); write_pidfile();" is common pattern
used by many services and even in library functions.

I can ask nginx developers to fix bug, but don't see bugs in nginx code.

Or this is bug in systemd? And may be systemd doing something wrong?

I can't understand the root cause of these two error messages.

Can you please help me,

How to run forking daemon with systemd in correct way, without errors?

Unit file of nginx is simple:

/usr/lib/systemd/system/nginx.service

=====================================================================

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

=====================================================================

-- 
Best regards,
  Gena


More information about the systemd-devel mailing list