Managing snap services

Snaps can define commands to be run as services (via systemd).

The list of services defined in the snap, along with their status, is accessible via SnapServices.list().This returns a dict with SnapService instances by service name.

The SnapService class allows interacting with a specific service:

>>> from snaphelpers import SnapServices
>>> services = SnapServices()
>>> services.list()
{'service1': <snaphelpers._service.SnapService object at 0x7fa4da982cc0>, 'service2': <snaphelpers._service.SnapService object at 0x7fa4da982c50>}
>>> service1 = services.list()['service1']
>>> service1.enabled,
(True, False)
>>> service1.notes
>>> service1.start()
>>> service1.enabled,
(True, True)
>>> service1.stop(disable=True)
>>> service1.enabled,
(False, False)

It’s also possible to start/stop/restart all services from the SnapServices instance:

>>> services.start(enable=True)
>>> [(,, s.enabled) for s in services.list().values()]
[('service1', True, True), ('service2', True, True)]
>>> services.stop(disable=True)
>>> [(,, s.enabled) for s in services.list().values()]
[('service1', False, False), ('service2', False, False)]