[arch-general] Systemd email notifications

Paul Gideon Dann pdgiddie at gmail.com
Thu Feb 13 10:31:52 EST 2014


On Thursday 13 Feb 2014 16:05:05 Rodrigo Rivas wrote:
> Ok... I'll take the chance to practice my DBus abilities...
> It is a bit long, but it kind of works. Just replace the print() call
> with your favourite sendmail function and you'll get a notification
> every time any of the units specified in the command line changes
> status.
> 
> HTH.
> 
> #!/usr/bin/python
> from gi.repository import GObject
> import sys
> import dbus
> from dbus.mainloop.glib import DBusGMainLoop
> 
> DBusGMainLoop(set_as_default=True)
> 
> bus = dbus.SystemBus()
> systemd = bus.get_object('org.freedesktop.systemd1',
> '/org/freedesktop/systemd1')
> manager = dbus.Interface(systemd, 'org.freedesktop.systemd1.Manager')
> 
> def GetPropChanged(obj, iface, propName, changes, invalids):
>     if propName in invalids:
>         return obj.Get(iface, propName,
> dbus_interface=dbus.PROPERTIES_IFACE) elif propName in changes:
>         return changes[propName]
>     else:
>         return None
> 
> def OnPropChanged(unit, name, iface, changes, invalids):
>     if iface != 'org.freedesktop.systemd1.Unit':
>         return
>     state = GetPropChanged(unit, iface, 'ActiveState', changes, invalids)
>     substate = GetPropChanged(unit, iface, 'SubState', changes, invalids)
>     if state or substate:
>         print('Status changed', name, state, substate)
> 
> for unitName in sys.argv[1:]:
>     unit = bus.get_object('org.freedesktop.systemd1',
> manager.GetUnit(unitName)) unit.connect_to_signal('PropertiesChanged',
> lambda a,b,c :
> OnPropChanged(unit, unitName, a, b, c),
> dbus_interface=dbus.PROPERTIES_IFACE)
> 
> loop = GObject.MainLoop()
> loop.run()

Thanks Rodrigo; great work.  I may come back to this in future if no "official" solution 
ever seems forthcoming.

Paul


More information about the arch-general mailing list