If a systemd service unit is in a failed state, systemctl status shows that fact:
# systemctl status myservice
● myservice.service - Important Crash Prone service (myservice)
Loaded: loaded (/etc/systemd/system/myservice.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Tue 2017-08-22 20:04:13 UTC; 3s ago
Process: 31108 ExecStart=/bin/myservice (code=dumped, signal=ABRT)
Main PID: 31108 (code=dumped, signal=ABRT)
I'd like to have a Python program get that "Result: start-limit" field without forking systemctl and attempting to parse the output. The other fields (e.g. Loaded, Active) are available from the Python dbus library.
For instance, the following code gets the ActiveState property which is displayed by systemctl after "Active:" (the word "failed" in the output above).
from dbus import Interface, SystemBus, SessionBus
bus = SystemBus()
systemd = bus.get_object('org.freedesktop.systemd1',
'/org/freedesktop/systemd1')
manager = Interface(systemd, dbus_interface='org.freedesktop.systemd1.Manager')
myservice_unit = manager.LoadUnit('myservice.service')
myservice_proxy = bus.get_object('org.freedesktop.systemd1', str(myservice_unit))
myservice = Interface(myservice_proxy,
dbus_interface='org.freedesktop.systemd1.Unit')
prop = 'ActiveState'
value = myservice_proxy.Get('org.freedesktop.systemd1.Unit',
prop,
dbus_interface='org.freedesktop.DBus.Properties')
print("{} is: {}".format(prop, value))
It appears the "Result" value is not a property in the sense that much of the other output is.
If you check the docs at https://www.freedesktop.org/wiki/Software/systemd/dbus/, it explains the Result being part of Service Unit Objects (org.freedesktop.systemd1.Service).
So, I added this to your code:
if value == 'failed':
result_prop = 'Result'
result = myservice_proxy.Get('org.freedesktop.systemd1.Service',
result_prop,
dbus_interface='org.freedesktop.DBus.Properties')
print(result)
Now it's working beautifully!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With