如何在 dbus 信号上运行脚本?

Eri*_*ami 16 automation dbus

是否可以在任意 dbus 信号上运行脚本?像 Upstart 这样的东西以非特权用户身份运行并且不需要 root 权限来修改?

我问是因为我已经写了一个愚蠢的脚本,等待蓝牙事件启动我的音乐播放器。现在,当我的机器连接到特定网络或连接其他设备时,我想做类似的事情。

编辑:我最初的问题没有具体说明这一点,但我的意思是“将多个脚本与一组事件中的一个相关联”-所以我有相同的启动器-y 东西(如 Upstart)来管理多个脚本,每个都关心不同的 dbus 信号。都在用户空间。

int*_*_ua 20

你需要 dbus-monitor

脚本看起来像

#!/bin/bash

interface=org.gnome.Rhythmbox.Player
member=playingUriChanged

# listen for playingUriChanged DBus events,
# each time we enter the loop, we just got an event
# so handle the event, e.g. by printing the artist and title
# see rhythmbox-client --print-playing-format for more output options

dbus-monitor --profile "interface='$interface',member='$member'" |
while read -r line; do
  printf "Now playing: "
  rhythmbox-client --print-playing
done
Run Code Online (Sandbox Code Playgroud)

取自stackoverflow


小智 5

我为此目的编写了一个简单的工具:https ://github.com/jluttine/dbus-listen

与其他答案中相同的示例如下所示。编写收到信号时应执行的脚本:

#!/bin/bash
printf "Now playing: "
rhythmbox-client --print-playing
Run Code Online (Sandbox Code Playgroud)

监听信号(假设上面的脚本名为myscript.sh):

dbus-listen --interface org.gnome.Rhythmbox.Player --member playingUriChanged myscript.sh
Run Code Online (Sandbox Code Playgroud)

当您需要监听系统总线时,使用另一个答案dbus-monitor不起作用。