Run a dbus program in crontab , how to know about the SESSION id?

I need to run some program within crontab , but how can the program know about dbus session id ? it’s only available for programs launched by session managers.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The problem is somewhat similar to accessing the X display and finding the location of the X cookie file. (Also, refer to these questions if you want to launch a GUI program on the user’s display.)

Dbus stores the session address in a file in ~/.dbus/session-bus. The name of the file is $machine_id-$display_number, where $machine_id is a randomly generated number stored in /var/lib/dbus/machine-id and $display_number is the X display number ($DISPLAY is :$display_number or :$display_number.$screen_number). The file in ~/.dbus/session-bus is parseable by a shell and contains definitions for DBUS_SESSION_BUS_ADDRESS and DBUS_SESSION_BUS_PID.

dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
if [ -e "$dbus_session_file" ]; then
  . "$dbus_session_file"
  dbus-send …

Beware that there’s no guarantee that the dbus daemon is still available. The user may have logged out.

An alternative method is to find the PID of a process in the desktop session, and obtain the dbus address from its environment.

export $(</proc/$pid/environ tr \0 \n | grep -E '^DBUS_SESSION_BUS_ADDRESS=')

If the crontab is running as root and you want to communicate with the session of whatever user is logged in on the console, see Can I launch a graphical program on another user’s desktop as root?

Method 2

I can’t comment on Vincenzo’s answer, but I find his answer works best for me on KDE4.

I’ve had to slightly modify the command though. For me it’s:

ps -u yourlogin e | grep -Eo 'dbus-daemon.*ADDRESS=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35

Notice ADDRESS in capital letters.

Method 3

I think if you know the pid of the gnome session manager, then you read the environment from /proc filesystem.


Use the SESSION id then, with other programs like notify-send or dbus* tools.


Method 4

ps -u yourlogin e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35

Method 5

Yes DBus must have a x session.
Like earlier answers, ‘DBUS_SESSION_BUS_ADDRESS’ must have a value before starting . However, you could use ‘dbus-launch’ to create this value. Following snippet could be put in a script called from crontab.

if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
  eval `dbus-launch --sh-syntax`
echo "D-Bus daemon address is:"

# -E to export DBUS_SESSION_BUS_ADDRESS. Crucial to make DBUS work.
sudo -u <user> -E <program>

One setback with this solution is that the session might hang around after you’ve stopped your software.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments