Starting users systemd service via python and dbus

To manage services, you can use the systemd manager via dbus, for example: Starting the systemd service through python using this:

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('test.service', 'fail') 

However, systemd can also process user service files using the --user flag, for example:

 systemctl --user start test.service 

How can I use a user manager from dbus (using python)? Replacing dbus.SystemBus() with dbus.Bus() or dbus.SessionBus() did not do the trick, as this gives:

 dbus.exceptions.DBusException: org.freedesktop.DBus.Error.Spawn.ChildExited: Process org.freedesktop.systemd1 exited with status 1 
0
python systemd dbus
source share
1 answer

dbus.SessionBus () is the correct method to connect to the session bus, according to https://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.txt

Based on the above error message, I suspect you have a different problem with the dbus and / or your session. For example, when I try to connect without a valid session, I get a slightly different exception, org.freedesktop.DBus.Error.NotSupported

I would check the systemd logs for more information.

+2
source share

All Articles