edit

Dbuscron

event Sat 18 Dec '10
language en
code code

Upd: I've just release version 1.3. You can download deb-package for Maemo 5. The build includes one new feature and some improvements in log messages and error handling:

The main feature of the build is two new options --userid and --groupid. They make daemon change its process's user and group id. You may need it for two reasons: for security, and to run the daemon on other user's sessions bus. These options work only if dbuscron is run by the root itself, of cause. You can pass either numeric id of user/group or their name as value for these options. If you pass --userid only, the process's group will be set to main group of given user.


Upd: The new 1.2 version is out. You can download deb-package for Maemo 5. It includes some fixes and code improvements:

Besides that I've forgotten to mention dbuscrontab utility used to edit dbuscron's config flawlessly. It works just like crontab utility from good old cron daemon. It accepts one of following arguments:

The most useful argument is -e. Use dbuscrontab -e to edit system-wide config. The reasons a many:

  1. it edits copy of /etc/dbuscrontab, not this file itself, so you are protected from system crashes like power or software failures,
  2. it replaces original config with updated copy only if editor is finished correctly and the copy is really changed,
  3. and the last, but the most important thing: it checks copy you changed for syntax errors before overwriting system-wide config, so even you make some mistake your main config will stay safe and intact.

And here are dbuscron's arguments:


After several cleanups I decided to present my cron-like DBUS messages scheduler to community. I made it for my N900, so deb-package is created for Maemo 5, but the daemon itself can be used on any Linux-based OS with DBUS. Written in Python.

It work pretty simply: just run dbuscron and be happy =)

But you better config it before running to make it useful. Main dbuscron's config file is /etc/dbuscrontab and looks like crontab files. Every line is a rule to match DBUS messages. These rules consist of nine parts: bus (either “S” for system bus or “s” for session bus), message type (signal, method_call, method_return or error), source name, interface name, object path, member method name, destination name, message arguments and command to run on matched message.

Every field can have many values to match separated with comma, or can have single star (*) to match any value. Arguments field can have several argument values separated with semi-colon, rule will match message only if all rule argument values match DBUS message argument values.

You can also set environment variables in any place of config as “NAME=VALUE” pairs, one variable per line.

Besides that these environment variables are set by dbuscron for commands run on matched message:

Empty lines and lines starting with “#” are ignored as always =)

I can tell you about abilities of the daemon for many hours, but example of my own config is more useful in this case:

# Stop player on headphones plug out
S signal * org.freedesktop.Hal.Device /org/freedesktop/Hal/devices/platform_headphone Condition * ButtonPressed;connection grep -q disconnected /sys/devices/platform/gpio-switch/headphone/state && run-standalone.sh /opt/userscripts/mpcontrol.sh stop
# Say incoming number with espeak
S signal * com.nokia.csd.Call /com/nokia/csd/call Coming * * run-standalone.sh /opt/userscripts/tasks/speak-caller.sh
# Connect to ISP via PPTP on home WiFi network connection
S signal * com.nokia.wlancond.signal /com/nokia/wlancond/signal connected * wlan0 run-standalone.sh /opt/userscripts/tasks/connect-pptp.sh
# Disconnect PPTP
S signal * com.nokia.wlancond.signal /com/nokia/wlancond/signal disconnected * wlan0 run-standalone.sh /opt/userscripts/tasks/disconnect-pptp.sh
# Switch to 3G mode on GPRS connected to make it faster
S signal * com.nokia.csd.GPRS.Context /com/nokia/csd/gprs/0 Connected * * /opt/userscripts/radiomode.sh both
# Switch back to GSM mode only on GPRS disconnected to save battery
S signal * com.nokia.csd.GPRS.Context /com/nokia/csd/gprs/0 Disconnected * * /opt/userscripts/radiomode.sh gsm
# This message comes on cell changed:
#S signal * Phone.Net /com/nokia/phone/net cell_info_change * status;lac;cid;mnc;mcc;services;userdata command
# This message comes on operator name changed:
#S signal * Phone.Net /com/nokia/phone/net operator_name_change * status;opname;unk;mnc;mcc command
# Show notification message on operator name changed:
S signal * Phone.Net /com/nokia/phone/net operator_name_change * * run-standalone.sh /opt/userscripts/tasks/show-opname.sh

That's it, there're really many possibilities as you can see =)

This daemon can be found on my github account here: http://github.com/kstep/dbuscron. Or you can download deb-package for Maemo 5 from here.