<div dir="ltr">

<p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">Hi All,<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span> </span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">We are facing issue in on system services.<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span> </span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">Problem statement:<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">We have two process P1 and P2, both are getting daemonize using fork() command and parent exits immediately after fork().<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">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)<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span> </span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">For that, I have created two systemd service files as<span> </span><b>: P1.service</b><span> </span>and<span> </span><b>P2.service</b><span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><b><span style="color:rgb(31,77,120)">------------------------------<wbr>---------------------------<span></span></span></b></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><b><span style="color:rgb(31,77,120)">P1.service<span></span></span></b></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">[Unit]<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">Description=P1 service<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)"><span> </span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">[Service]<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">Type=notify<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">NotifyAccess=main<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">PIDFile=/var/tmp/P1.pid<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">ExecStart<span style="background:yellow">=/usr/bin/P1</span><span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">Restart=always<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)"><span> </span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">StandardOutput=syslog+console<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)"><span> </span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">[Install]<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">WantedBy=multi-user.target<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span> </span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span> </span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><b><span style="color:rgb(31,77,120)">P2.service<span></span></span></b></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">[Unit]<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">Description=P2 service<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120);background:yellow">After=P1.service</span></i><i><span style="font-size:9pt;color:rgb(31,77,120)"><span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)"><span> </span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">[Service]<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">Type=notify<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">NotifyAccess=main<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">PIDFile=/var/tmp/P2.pid<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">ExecStart<span style="background:yellow">=/usr/bin/P2</span><span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">Restart=always<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)"><span> </span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">StandardOutput=syslog+console<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)"><span> </span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">[Install]<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><i><span style="font-size:9pt;color:rgb(31,77,120)">WantedBy=multi-user.target<span></span></span></i></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">------------------------------<wbr>------------------------------<wbr>----<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span> </span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">We want process P2  to start once P1 is started successfully, for that we have defined “</span><i><span style="font-size:9pt;color:rgb(31,77,120)">After=P1.service”  </span></i><span style="color:rgb(31,77,120)">in P2.service file</span><span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span> </span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">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.<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span> </span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">Issue we are facing:<span></span></span></p><p class="gmail-m_5681265205561188752gmail-MsoListParagraph" style="text-decoration-style:initial;text-decoration-color:initial;margin:0in 0in 0.0001pt 0.5in;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span>1.<span style="font:7pt "Times New Roman"">     <span> </span></span></span></span><span style="color:rgb(31,77,120)">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).<span></span></span></p><p class="gmail-m_5681265205561188752gmail-MsoListParagraph" style="text-decoration-style:initial;text-decoration-color:initial;margin:0in 0in 0.0001pt 0.5in;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span>2.<span style="font:7pt "Times New Roman"">     <span> </span></span></span></span><span style="color:rgb(31,77,120)">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.<span></span></span></p><p class="gmail-m_5681265205561188752gmail-MsoListParagraph" style="text-decoration-style:initial;text-decoration-color:initial;margin:0in 0in 0.0001pt 0.5in;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span> </span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">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.<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span> </span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">If there is a way to handle such scenario appropriately in system, your help will be appreciated.</span><span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span> </span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)"><span> </span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">Thanks,<span></span></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt;text-decoration-style:initial;text-decoration-color:initial;font-size:11pt;font-family:Calibri,sans-serif"><span style="color:rgb(31,77,120)">Rajesh</span></p>

<br></div>