query on systemd services ordering for daemonize process
Rajesh Kumar
myemail.rajeshkumar at gmail.com
Fri Aug 3 16:29:41 UTC 2018
Hi All,
We are facing issue in on system services.
Problem statement:
We have two process P1 and P2, both are getting daemonize using fork()
command and parent exits immediately after fork().
We want process P2 to start once P1 is initialized successfully (means P2
should start when P1 is listening on particular socket and in ready state
to handle request)
For that, I have created two systemd service files as *: P1.service*
and *P2.service
*
*---------------------------------------------------------*
*P1.service*
*[Unit]*
*Description=P1 service*
*[Service]*
*Type=notify*
*NotifyAccess=main*
*PIDFile=/var/tmp/P1.pid*
*ExecStart=/usr/bin/P1*
*Restart=always*
*StandardOutput=syslog+console*
*[Install]*
*WantedBy=multi-user.target*
*P2.service*
*[Unit]*
*Description=P2 service*
*After=P1.service*
*[Service]*
*Type=notify*
*NotifyAccess=main*
*PIDFile=/var/tmp/P2.pid*
*ExecStart=/usr/bin/P2*
*Restart=always*
*StandardOutput=syslog+console*
*[Install]*
*WantedBy=multi-user.target*
----------------------------------------------------------------
We want process P2 to start once P1 is started successfully, for that we
have defined “*After=P1.service” *in P2.service file
Our processes P1 and P2 which are started through service file are also
getting demonized using fork(). And, we have used sd_notify() mechanisum to
notify systemd once process initialization completes.
Issue we are facing:
1. Systemd also starts P2.service once hold off time of P1.service
expires and restart of P1.service is triggered by systemd .we are using
default hold-off time i.e. 10 sec. If P1 fails to send sd_notify within 10
sec then systemd is restarting P1.service and also starting P2.service(this
service was supposed to start once P1 initialization completes but when
systemd is not getting sd_notify from P1 and timeout happens, then also
“After” dependency of P2.service is getting met and P2.service is getting
started).
2. As our P1 and P2 process are getting demonized (by using fork()
system call), sometime, we have also seen that when parent process exits
(as a part of demonizing a process, we have used fork() and then
immediately parent process has to exit), then also systemd restarts
P1.service (and P2.service), and other services (which are dependent on
these service using “AFTER”) are also getting started.
We were able to achieve the correct ordering of systemd services using Path
unit and by waiting for file to be created on filesystem to ensure
initialization of particular process has been compete. But because of some
restriction we can’t rely on files written to file system to coordinate
initialization or dependencies.
If there is a way to handle such scenario appropriately in system, your
help will be appreciated.
Thanks,
Rajesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/xdg/attachments/20180803/42f8ce47/attachment.html>
More information about the xdg
mailing list