Starting a users systemd service via python and dbus

故事扮演 提交于 2019-12-12 04:19:58

问题


It is possible to use systemd's manager via dbus to control services, e.g: Starting a systemd service via 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 handle user's service files with the --user flag, e.g.:

systemctl --user start test.service

How can the user's manager be used from dbus (with 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

回答1:


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

Based on the error message shown I suspect some other issue with dbus and/or your session. For example, when attempting to connect without a valid session I get a slightly different exception, org.freedesktop.DBus.Error.NotSupported

I would check systemd logs for further information.



来源:https://stackoverflow.com/questions/42088406/starting-a-users-systemd-service-via-python-and-dbus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!