PyNotify not working from cron?

I wrote a script that uses pynotify for warning. it works fine when I run it ( python script.py ), but when cron starts with 00 * * * * myname python ~/scripts/script.py it doesn't work! I do not know why. Here is a snippet:

  if os.path.isfile(os.path.expanduser('~/.thumbnails/normal')+'/'+thumbnail): n = pynotify.Notification(video_file[0], 'finished download', os.path.expanduser('~/.thumbnails')+'/'+thumbnail) else: n = pynotify.Notification(video_file[0], 'finished download', '/usr/share/icons/gnome/48x48/mimetypes/gnome-mime-application-x-shockwave-flash.png') print n n.show() 

redirecting output to ~ / log.file gives: <pynotify.Notification object at 0x16d4e60 (NotifyNotification at 0x13804e0)> and no errors, so I'm not quite sure where else to look.

+4
source share
1 answer

I'm not that deep in cron, but I know a little about pynotify. It uses libnotify and some DBUS stuff, so somewhere it makes a call to DBUS, and iirc also passes the display identifier on which the notification should be displayed.

Now, by default, cron does not work with GUI applications, you must specify a mapping for them:

 00 * * * * myname env DISPLAY=:0 python ~/scripts/script.py 

This will force cron to use the current display (desktop).

If you are running Ubuntu, this page might interest you:
https://help.ubuntu.com/community/CronHowto

+5
source

All Articles