python - 为什么 dbus 服务无法正常运行?

标签 python linux service systemd dbus

我调查了 this , this , thisthis找不到答案。我正在尝试实现 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 时,一切正常。

最佳答案

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

单元文件或服务文件都没有执行实际声明给定总线名称 (org.mybus.demo.test) 的程序,这将导致 systemd 认为单元尚未启动适本地。 ExecStart= 执行的程序必须声明 D-Bus 名称本身。

关于python - 为什么 dbus 服务无法正常运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554678/

相关文章:

python - Django 应用程序内存使用情况

linux - linux 中的多个 sed -e 命令用法

linux - 将参数传递给 systemd-nspawn 容器

android - 启动和停止notificationlistener服务

Windows 服务启动参数

python - 在 Spark 中将简单的单行字符串转换为 RDD

python - "the interpreter inserts a newline before it prints the next prompt if the last line was not completed."是什么意思

python - 如何建立一对多模型?

android - mipsel-linux-gcc : Command not found

java - 聚合器模式,SOA(SplitJoin OSB)