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
  • You could use sendmail to notify the user of the results of the program. Commented Jul 21, 2010 at 18:02
  • sendmail would work, but it's just as effective as using a log to store results, which unfortunately isn't really what I'm looking for. Commented Jul 21, 2010 at 18:03

4 Answers 4

4
* * * * * 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

Sign up to request clarification or add additional context in comments.

1 Comment

I think that it would be helpful for others if you could explain each part of this line and what it does.
2

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.

1 Comment

That makes sense, but I would like to take advantage of the notifications, is there no way to do this from the crontab?
2

I just wanted to mention that the following recipe works for users of the awesome window manager:

*/1 * * * * DBUS_SESSION_BUS_ADDRESS=$(grep -zi DBUS /proc/$(pgrep awesome)/environ | sed -r -e 's/^DBUS_SESSION_BUS_ADDRESS=//') DISPLAY=":0.0" notify-send -t 0 blah blah

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.