使用gdbus启动systemd服务

时间:2015-07-14 19:30:31

标签: dbus systemd gdbus

我已经创建了一个新的systemd服务,我希望能够通过dbus调用激活它。该服务只执行shell脚本。

我在这里定义了服务:

/lib/systemd/system/testamundo.service


[Unit]
Description=Testamundo

[Service]
Type=dbus
BusName=org.freedesktop.testamundo
ExecStart=/home/test/systemd/testamundo.sh

我也在这里为它定义了一个D-Bus服务:

/usr/share/dbus-1/system-services

[D-BUS Service]
Name=org.freedesktop.testamundo
Exec=/usr/sbin/console-kit-daemon --no-daemon
User=root
SystemdService=testamundo.service

我正在尝试使用gdbus启动它,这是我正在尝试使用的命令:

sudo gdbus call --system --dest org.freedesktop.systemd1 --object-path /org/freedesktop/systemd1 --method org.freedesktop.systemd1.StartUnit "org.freedesktop.testamundo"

如果我像上面那样使用--system,那么命令将返回一个Unknown Method错误,如果我使用--session,则返回子进程的退出代码1。当我用--session和--system查看journalctl时,我可以看到命令,但除此之外没有其他信息。

感谢任何想法或建议,谢谢!

1 个答案:

答案 0 :(得分:3)

您的dbus命令正在使用不存在的接口。首先,org.freedesktop.systemd1.Manager.Start单位不是org.freedesktop.systemd1.StartUnit。其次,org.freedesktop.systemd1.Manager.Start需要2个参数,服务名称和启动模式。参考:http://www.freedesktop.org/wiki/Software/systemd/dbus/

您已定义了dbus服务,但是您通过直接要求systemd激活服务来绕过dbus。其他注意事项是,dbus实际上向systemd发送信号而不是方法调用。

你已经掌握了一切,如果你只是对你的服务进行反省,就应该激活它。

sudo gdbus call --system --dest org.freedesktop.testamundo --object-path /org/freedesktop/testamundo --method org.freedesktop.DBus.Introspectable. Introspect