简体   繁体   English

通过 python 和 dbus 启动用户 systemd 服务

[英]Starting a users systemd service via python and dbus

It is possible to use systemd's manager via dbus to control services, eg: Starting a systemd service via python using this:可以通过 dbus 使用 systemd 的管理器来控制服务,例如:使用以下命令通过 python 启动 systemd 服务

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, eg:但是,systemd 也可以使用--user标志处理用户的服务文件,例如:

systemctl --user start test.service

How can the user's manager be used from dbus (with python)?如何从 dbus(使用 python)使用用户的管理器? Replacing dbus.SystemBus() with dbus.Bus() or dbus.SessionBus() did not do the trick, as this gives:dbus.Bus()dbus.SessionBus() dbus.SystemBus()没有解决问题,因为这给出了:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.Spawn.ChildExited: Process org.freedesktop.systemd1 exited with status 1

dbus.SessionBus() is the correct method to connect to the session bus, as per dbus-python tutorial根据dbus-python 教程,dbus.SessionBus() 是连接到会话总线的正确方法

Based on the error message shown I suspect some other issue with dbus and/or your session.根据显示的错误消息,我怀疑 dbus 和/或您的会话存在其他问题。 For example, when attempting to connect without a valid session I get a slightly different exception, org.freedesktop.DBus.Error.NotSupported例如,当尝试在没有有效会话的情况下进行连接时,我得到一个略有不同的异常,org.freedesktop.DBus.Error.NotSupported

I would check systemd logs for further information.我会检查 systemd 日志以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM