<div dir="ltr"><p class="MsoNormal"><br></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">Hello systemd-experts,</span></p>
<p class="MsoNormal"><font color="#000000" face="Calibri, sans-serif"><span style="font-size:14px">I am new to systemd and dbus.</span></font></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">I am writing a daemon which
starts, stops and monitor services and I am using dbus to interface with
systemd. I send messages on dbus to start or stop the service and this is
working well. I want to achieve the following:</span></p>
<p class="MsoNormal"><br></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">a) Monitor service exit
using the systemd dbus interface.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">b) Monitor when the
service has entered a failed state (the app has been restarted n times in m
seconds).</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">I am trying to
understand what is the right mechanism to implement this using the systemd dbus
interface, I have tried a few things clearly I don’t understand how this works,
any help greatly appreciated.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">In my first attempt I
subscribed to JobRemoved signal from systemd as explained here (<a href="http://www.freedesktop.org/wiki/Software/systemd/dbus/">http://www.freedesktop.org/wiki/Software/systemd/dbus/</a>).
I was able to figure out when the service failed by looking at the result
string.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">———</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">JobNew() and
JobRemoved() are sent out each time a new job is queued or dequeued. Both
signals take the numeric job ID, the bus path and the primary unit name for
this job as argument. JobRemoved() also includes a result string, being one of
done, canceled, timeout, failed, dependency, skipped. done indicates successful
execution of a job. canceled indicates that a job has been canceled (via
CancelJob() above) before it finished execution (this doesn't necessarily mean
though that the job operation is actually cancelled too, see above). timeout
indicates that the job timeout was reached. failed indicates that the job
failed. dependency indicates that a job this job has been depending on failed
and the job hence has been removed too. skipped indicates that a job was
skipped because it didn't apply to the units current state.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">——</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">I soon realized that I
was getting spurious JobRemoved signals.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">I am now trying to
achieve (1) and (2) by subscribing to PropertiesChanged signal and I have a few
questions here:</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">1) When I get the
PropertiesChanged, I query the SubState property to get the running/stop state
of the service. Is querying the SubState property the right way to get the
service status? If SubState value is “running” then I infer that app is running
and its any other value, I infer the app is down. I am not relying on
ActiveState because I see that for some signal, ActiveState is “active” but
SubState as “exited”.</span></p><p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> Is this approach correct?</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">2) When a service exits,
I get multiple PropertiesChanged signal.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> kill -9
<myservice> will transition myservice to “stop”,
“auto-restart” and then “running” SubState</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> systemctl
restart myservice will transition myservice to “stop”, “stop-sigterm”
and then “running” SubState.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> systemctl
stop myservice will transition myservice to “stop” and then
“stop-sigterm” SubState.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">What does “stop” and
“stop-term” mean here and why are there 2 signals to indicate stop? Is “stop” a
good indicator that the service has stopped?</span></p>
<p class="MsoNormal"><span style="color:black;font-family:Calibri,sans-serif;font-size:10.5pt">In my client, I can
cache the services and their states. </span><br></p><p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"><br></span></p><p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">3)How do I get to know when an application
has failed (failed here means when systemd will not restart application again
after n app exits in m seconds).</span></p><p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">When I was using JobRemoved, I used the value
of “failed” in “result” parameter in JobRemoved signal, was this the correct
indicator to determine service failure?</span></p>
<p class="MsoNormal"><br></p><p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"><br></span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">4) How do I get to know
when an service has started? When I issue “systemctl start myservice”, I do not
getany PropertiesChanged signal, I receive JobNew, JobRemoved and then UnitNew
signals.</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">I thought of subscribing
to UnitNew signal. But I also get these multiple (i.e 2) UnitNew and UnitRemoved
signal when I do ‘systemctl stop myservice’ too. Why do I get UnitNew
when a service is being stopped?</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">From my research I
understand that requesting the properties of an unloaded unit will cause
systemd to send a pair of UnitNew/UnitRemoved signals and this may lead to
infinite loop. </span></p><p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">How do I fix this?</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">Am I solving the
requirement (a) and (b) correctly or should I be using a different mechanism to
achieve (a) and (b).</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">Appreciate your help on
above queries</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> </span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">Thanks</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black">Prashant</span></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Calibri,sans-serif;color:black"> <br></span></p></div>