【问题标题】:Why is dbus service not functioning correctly?为什么 dbus 服务无法正常运行?
【发布时间】:2019-04-07 00:36:25
【问题描述】:

我查看了thisthisthisthis,但找不到答案。我正在尝试从 systemd.service 实现示例 5,但它不起作用。当然原因是我不太了解 dbus 激活是如何工作的。在我看来,一旦开始使用 DBus 名称,就会激活服务并运行我的程序。我离得太远了吗?

所以,为了测试,我希望当我运行“使用”dbus 名称的 python3 程序时,我的服务会启动。

不管怎样,谁能指引我到正确的地方? 我的文件:

# cat /etc/systemd/system/mydbus.service
[Unit]
Description=Service Started by DBus name

[Service]
Type=dbus
BusName=org.mybus.demo.test
ExecStart=/bin/echo started
# ExecStart=/usr/bin/dbus-launch /usr/bin/python3 /home/myuser/Documents/dbus/client04.py
# the idea is that my python client up here will run once the server starts "consuming" the dbus name

[Install]
WantedBy=multi-user.target

以下是我的 dbus 服务(我认为):

# cat /usr/share/dbus-1/system-services/org.mybus.demo.test.service 
[D-BUS Service]
Name=org.mybus.demo.test
Exec=/bin/echo started >> /home/myuser/Documents/dbus/org.mybus.demo.test
User=root
SystemdService=mydbus.service

现在我运行$ sudo journalctl -exfu mydbus。在另一个终端上,我启动了我的server04.py

# cat /home/myuser/Documents/dbus/server04.py 
# Importing
from pydbus import SessionBus
from gi.repository import GLib
import time

# Variables / Constants / Instantiation...
bus = SessionBus()
BUS = "org.mybus.demo.test"
loop = GLib.MainLoop()
message_count = 0

class DBusService_XML():
    """
    DBus Service XML Definition.
    type = "i" for integer, "s" for string, "d" for double, "as" list of string data.
    """
    dbus = """
    <node>
        <interface name="{}">
            <method name='greeting'>
                <arg type="s" name="input" direction="in">
                </arg>
                <arg type="s" name="output" direction="out">
                </arg>
            </method>
        </interface>
    </node>
    """.format(BUS)

    def greeting(self, clientName):
        "Receive and send arg"
        print("{} is asking for name".format(clientName))
        return "Hello {}, Im Kyle".format(clientName)

if __name__ == "__main__":
    bus.publish(BUS, DBusService_XML())
    loop.run()

我虽然这样做,我的mydbus.service 会启动,我会在 journalctl 中看到“启动”,但什么也没发生。那么,我该怎么做呢?

PS.:当然,当我使用 python 手动运行 server04.py 和 client04.py 时,一切正常。

【问题讨论】:

    标签: python linux service systemd dbus


    【解决方案1】:

    您的server04.py 正在使用会话总线,但您提供的 systemd 单元文件和 D-Bus 服务文件是用于系统总线的。

    单元文件或服务文件都不会执行实际声明给定总线名称 (org.mybus.demo.test) 的程序,这将导致 systemd 认为单元未正确启动。 ExecStart= 执行的程序必须自己声明 D-Bus 名称。

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2015-03-24
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多