16

Is it possible to run a script on an arbitrary dbus signal? Something like Upstart that runs as an unprivileged user and doesn't require root perms to modify?

I ask because I've already written a silly script that waits for bluetooth events to start my music player. Now I'd like to do something similar when my machine connects to specific networks, or other devices attach.

EDIT: My original question didn't specify this, but I meant "associate a number of scripts with one of a set of events" - so I'd have the same launcher-y thing (like Upstart) that manages a number of scripts, each of which cares about a different dbus signal. All in user space.

1
  • Maybe it isn't exactly what you look for, but poped-up in my mind when you talked about upstart. You can trigger scripts with systemd. I'm not exactly sure what type of unit creates your bluetooth device, but I think there is one.
    – sumid
    Sep 29, 2015 at 9:03

2 Answers 2

21

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.

5

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .