我查看了this,this,this和this,但未找到答案。我正在尝试从systemd.service实现示例5,但它不起作用。当然原因是我不太明白dbus激活是如何工作的。在我看来,一旦DBus名称开始被使用,服务将被激活,我的程序将运行。我离得太远了吗?
所以,为了测试,我希望当我运行“使用”dbus名称的python程序时,我的服务将启动。
无论如何,任何人都可以指引我到正确的地方吗?我的文件:
# 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
正在使用会话总线,但您提供的systemd单元文件和D-Bus服务文件是用于系统总线的。
单元文件或服务文件都不执行实际声明给定总线名称(org.mybus.demo.test
)的程序,这将导致systemd认为该单元未正确启动。由ExecStart=
执行的程序必须自行声明D-Bus名称。