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

本文介绍如何通过dbus与python交互,控制systemd用户服务。当尝试使用dbus.SessionBus()代替dbus.SystemBus()时遇到错误,解决方法是检查dbus和会话是否存在其他问题,或查看systemd日志获取更多信息。
摘要由CSDN通过智能技术生成

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

解决方案

dbus.SessionBus() is the correct method to connect to the session bus, as per dbus-python tutorial

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值