1

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.
2
  • 1
    Have you read the docs on EnableUnitFiles()? Commented Sep 6, 2020 at 18:42
  • @Terry Actually I couldn't find the docs and I though they didn't managed to write them yet! Commented Sep 7, 2020 at 6:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.