From linuxer, 7 Months ago, written in Plain Text.
Embed
  1. def _is_service_active_dbus(system_bus, service_name):
  2.  
  3.     systemd = system_bus.get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
  4.  
  5.     try:
  6.         unit_path = systemd.GetUnit("%s.service" % service_name, dbus_interface="org.freedesktop.systemd1.Manager")
  7.     except dbus.exceptions.DBusException:
  8.         return False
  9.  
  10.     optimus_manager_interface = system_bus.get_object("org.freedesktop.systemd1", unit_path)
  11.     properties_manager = dbus.Interface(optimus_manager_interface, 'org.freedesktop.DBus.Properties')
  12.     state = properties_manager.Get("org.freedesktop.systemd1.Unit", "SubState")
  13.  
  14.     return state == "running"