How do I run a script on a dbus signal?

Solution 1:

You need the dbus-monitor

The script will look something like

#!/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

Taken from stackoverflow.

Solution 2:

I wrote a simple tool for this purpose: https://github.com/jluttine/dbus-listen

The same example as in the other answer could look as follows. Write the script that should be executed when the signal is received:

#!/bin/bash
printf "Now playing: "
rhythmbox-client --print-playing

Listen to the signal (assuming the above script is named myscript.sh):

dbus-listen --interface org.gnome.Rhythmbox.Player --member playingUriChanged myscript.sh

The other answer using dbus-monitor doesn't work when you need to listen to the system bus.