views:

165

answers:

3

I've got a python program which runs via crontab and that works perfectly. However, I decided to add the ability to notify me of what it's doing, and suddenly it's failing. It runs from the command line, however, running it as a crontab program causes it to fail

libnotify-Message: Unable to get session bus: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

What am I doing wrong?

Edit I would like this program to still run from cron and be able to take advantage of notifying the user of it's work. Is there any way to do this?

Edit 2 I've tried using root's crontab and sudo -u esr python script.py yet this also fails, silently at that.

Edit 3 It is possible! Here's the code.

* * * * * su $user -c "DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps -au esr | grep -i "gnome-session" | awk '{ print $1 }')/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') $(whereis notify-send | awk '{ print $2 }') -u normal -t 20000 \"Hello\" "
+2  A: 

You're trying to use GUI (GTK+ library calls) for cron program that has no access to graphical terminal. You need to avoid creating dialogs and windows when you run it from cron.

che
+2  A: 

You're trying to run a script that requires user resources in an environment where said resources are not available. You will have to strip the script of all references to PyGTK and to the session bus if you want this to work.

Ignacio Vazquez-Abrams
That makes sense, but I would like to take advantage of the notifications, is there no way to do this from the crontab?
EricR
+1  A: 
* * * * * su esr -c "DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps -au esr | grep -i "gnome-session" | awk '{ print $1 }')/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') $(whereis notify-send | awk '{ print $2 }') -u normal -t 20000 \"Hello\" "

As per a suggestion, an explanation, unfortunately not mine

EricR
I think that it would be helpful for others if you could explain each part of this line and what it does.
B Johnson
sure thing, done.
EricR