我现在正在使用适用于 golang 的 D-Bus API 调用并启动一个 systemd 服务(我的只是调用一个 shell 脚本)。
我在/usr/share/dbus-1/system-services/org.freedesktop.hello.service 做了一个 D-Bus 服务
[D-BUS Service]
Name=org.freedesktop.hello
Exec=/bin/false
User=root
SystemdService=hello.service
还有/lib/systemd/system/hello.service 中的 systemd 服务
[Unit]
Description=Hello
[Service]
Type=dbus
BusName=org.freedesktop.hello
ExecStart=/opt/hello.sh
我正在尝试使用下面的代码实现相同的结果,这很有效。
sudo gdbus call --system --dest org.freedesktop.hello --object-path /org/freedesktop/hello --method org.freedesktop.DBus.Introspectable.Introspect
但是我在 Golang 中不断收到错误,
The name org.freedesktop.hello was not provided by any .service files
我现在的代码是
package main
import (
"encoding/json"
"github.com/godbus/dbus"
"os"
"github.com/godbus/dbus/introspect"
)
func main() {
conn, error1 := dbus.SessionBus()
if error1 != nil {
panic(error1)
}
node, err2 := introspect.Call(conn.Object("org.freedesktop.hello", "/org/freedesktop/hello"))
if err2 != nil {
panic(err2)
}
data, _ := json.MarshalIndent(node, "", " ")
os.Stdout.Write(data)
}
关于这个东西的信息不多,所以我想得到一些帮助。谢谢!
最佳答案
sudo gdbus call --system ...
那是在系统总线上。
...
conn, error1 := dbus.SessionBus()
...
这是在 session 总线上。
尝试使用像 dbus.SystemBus()
这样的东西。
关于go - 使用 Golang 启动 D-Bus 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47320709/