According to Starting a systemd service via python I can use D-Bus API for starting/stopping Systemd services as following:
import dbus
sysbus = dbus.SystemBus()
systemd1 = sysbus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager')
job = manager.RestartUnit('sshd.service', 'fail')
But what if I want to enable a systemd service there. I already tried replacing RestartUnit
with EnableUnit
, but I got:
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Unknown method EnableUnit or interface org.freedesktop.systemd1.Manager.
OK. I figured it out thanks to Terry Spotts.
job = manager.EnableUnitFiles(['ssh.service'], False, True)
manager.Reload()
First argument is a list of systemd unit file names.
Second argument is a boolean controls whether the unit shall be enabled for runtime only (true, /run), or persistently (false, /etc).
Third argument is a boolean controls whether symlinks pointing to other units shall be replaced if necessary.