本文介绍了如何通过Systemd DBus API提取服务状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试使用 Systemd openvpn.service 状态wiki.freedesktop.org/www/Software/systemd/dbus/#unitobjects rel = nofollow noreferrer> D-Bus API 。 在[1]中:导入dbus 在[2]中:sysbus = dbus.SystemBus() 在[3]中:systemd1 = sysbus .get_object('org.freedesktop.systemd1','/ org / freedesktop / systemd1') 在[4]中:manager = dbus.Interface(systemd1,'org.freedesktop.systemd1.Manager' ) 在[5]中:service = dbus.Interface(systemd1,'org.freedesktop.systemd1.Service') 在[6]中:unit = dbus.Interface (systemd1,'org.freedesktop.systemd1.Unit') 在[7]中:unit.ActiveState('openvpn.service') ---------- -------------------------------------------------- --------------- DBusException追溯(最近一次调用为最新)< ipython-input-7-22857e7dcbd7>在< module>() ---->中1个unit.ActiveState('openvpn.service') /usr/local/lib/python3.4/dist-packages/dbus/proxies.py in __call __(self,* args,** keywords ) 68#我们正在同步,因此请封锁 69 self._block() ---> 70 return self._proxy_method(* args,** keywords) 71 72 def call_async(self,* args,** keywords): / usr / local / lib /python3.4/dist-packages/dbus/proxies.py in __call __(self,* args,** keywords) 143签名, 144 args,-> 145个**关键字) 146 147 def call_async(self,* args,**关键字): /usr/local/lib/python3.4/dist-packages /dbus/connection.py在call_blocking中(self,bus_name,object_path,dbus_interface,method,signature,args,timeout,byte_arrays,** kwargs) 649#进行阻塞调用 650 reply_message = self。 send_message_with_reply_and_block(-> 651消息,超时) 652 args_list = reply_message.get_args_list(** get_args_opts) 653 if len(args_list)== 0: DBusException:org.freedesktop.DBus.Error.UnknownMethod:未知方法 ActiveState或接口 org.freedesktop.systemd1.Unit。 输入[8]:manager.GetUnit('openvpn.service')输出[8]:dbus.ObjectPath('/ org / freedesktop / systemd1 / unit / openvpn_2eservice')$ b [b] [9]:u = manager.GetUnit('openvpn.service') [10]:u.ActiveState ------ -------------------------------------------------- ------------------- AttributeError追溯(最近一次呼叫最近)< ipython-input-10-6998e589f206>在< module>() ---->中1 u.ActiveState AttributeError:'dbus.ObjectPath'对象没有属性'ActiveState' 在[11]中:manager.GetUnitFileState('openvpn.service') Out [11]:dbus.String('enabled') 有一个 ActiveState 属性,其中包含反映该单元当前是否处于活动状态的状态值。我已经成功读取了 UnitFileState ,但是我不知道如何读取 ActiveState 属性。解决方案查看一些随机示例 [更多永久链接],我在 import dbus sysbus = dbus.SystemBus() systemd1 = sysbus.get_object('org.freedesktop.systemd1','/ org / freedesktop / systemd1')管理器= dbus.Interface(systemd1,'org.freedesktop.systemd1.Manager')服务= sysbus.get_object('org.freedesktop.systemd1 ', object_path = manager.GetUnit('openvpn.service'))接口ce = dbus.Interface(service, dbus_interface ='org.freedesktop.DBus.Properties') print(interface.Get('org.freedesktop.systemd1.Unit','ActiveState')) 记录版本,以防万一: dbus.version ==(1、2、14) systemd 244(244.1-1-arch) Python 3.8。 1 I'm trying to extract openvpn.service status using Systemd D-Bus API.In [1]: import dbusIn [2]: sysbus = dbus.SystemBus()In [3]: systemd1 = sysbus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')In [4]: manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager')In [5]: service = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Service')In [6]: unit = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Unit')In [7]: unit.ActiveState('openvpn.service')---------------------------------------------------------------------------DBusException Traceback (most recent call last)<ipython-input-7-22857e7dcbd7> in <module>()----> 1 unit.ActiveState('openvpn.service')/usr/local/lib/python3.4/dist-packages/dbus/proxies.py in __call__(self, *args, **keywords) 68 # we're being synchronous, so block 69 self._block()---> 70 return self._proxy_method(*args, **keywords) 71 72 def call_async(self, *args, **keywords):/usr/local/lib/python3.4/dist-packages/dbus/proxies.py in __call__(self, *args, **keywords) 143 signature, 144 args,--> 145 **keywords) 146 147 def call_async(self, *args, **keywords):/usr/local/lib/python3.4/dist-packages/dbus/connection.py in call_blocking(self, bus_name, object_path, dbus_interface, method, signature, args, timeout, byte_arrays, **kwargs) 649 # make a blocking call 650 reply_message = self.send_message_with_reply_and_block(--> 651 message, timeout) 652 args_list = reply_message.get_args_list(**get_args_opts) 653 if len(args_list) == 0:DBusException: org.freedesktop.DBus.Error.UnknownMethod: Unknown method 'ActiveState' or interface 'org.freedesktop.systemd1.Unit'.In [8]: manager.GetUnit('openvpn.service')Out[8]: dbus.ObjectPath('/org/freedesktop/systemd1/unit/openvpn_2eservice')In [9]: u = manager.GetUnit('openvpn.service')In [10]: u.ActiveState---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-10-6998e589f206> in <module>()----> 1 u.ActiveStateAttributeError: 'dbus.ObjectPath' object has no attribute 'ActiveState'In [11]: manager.GetUnitFileState('openvpn.service')Out[11]: dbus.String('enabled')There's an ActiveState property, which contains a state value that reflects whether the unit is currently active or not. I've succeeded in reading UnitFileState, however I can't figure out how to read ActiveState property. 解决方案 After having a look at some random examples [more permanent link], I've had some luck withimport dbussysbus = dbus.SystemBus()systemd1 = sysbus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager')service = sysbus.get_object('org.freedesktop.systemd1', object_path=manager.GetUnit('openvpn.service'))interface = dbus.Interface(service, dbus_interface='org.freedesktop.DBus.Properties')print(interface.Get('org.freedesktop.systemd1.Unit', 'ActiveState'))[Edit:]Documenting the versions, just in case:dbus.version == (1, 2, 14)systemd 244 (244.1-1-arch)Python 3.8.1 这篇关于如何通过Systemd DBus API提取服务状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-18 14:32
查看更多