Skip to content

Instantly share code, notes, and snippets.

@tchollingsworth
Created August 12, 2011 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tchollingsworth/1141585 to your computer and use it in GitHub Desktop.
Save tchollingsworth/1141585 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
import dbus, os, subprocess
bus = dbus.SystemBus()
manager_proxy = bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
manager = dbus.Interface(manager_proxy, 'org.freedesktop.systemd1.Manager')
restart = []
for unit in manager.ListUnits():
name = unit[0]
status = unit[3]
path = unit[6]
#is it supposed to be running and is it a service?
if status == 'active' and path.endswith("service"):
unit_proxy = bus.get_object('org.freedesktop.systemd1', path)
unit_if = dbus.Interface(unit_proxy, 'org.freedesktop.systemd1.Unit')
unit_props = dbus.Interface(unit_proxy, 'org.freedesktop.DBus.Properties')
pid = unit_props.Get('org.freedesktop.systemd1.Service', 'ExecMainPID')
type = unit_props.Get('org.freedesktop.systemd1.Service', 'Type')
#don't restart oneshot scripts, make sure we have a PID, and see if it's not running
if type == 'forking' and pid and not os.path.exists("/proc/{0}/".format(pid)):
#we need to restart them all in one transaction to prevent dependency issues
restart.append(name)
#just restart them with systemctl, dbus has given me enough of a headache for now
if restart:
subprocess.call(["/bin/systemctl", "restart"] + restart)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment