pythoncronpynotify

PyNotify not working from cron?


I've written a script that uses pynotify to give an alert. it works just fine when I run it (python script.py), but when run from cron with 00 * * * * myname python ~/scripts/script.py, it doesn't work! I haven't a clue why. Here's the 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()

directing the 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.


Solution

  • I'm not that deep into cron jobs, but I know a bit about pynotify. It uses libnotify and some DBUS stuff, so somewhere it makes the call to the DBUS and iirc it also passes the display id on which the notification should be shown.

    Now, by default cron's don't work with GUI applications, you have to specify a display for them to use:

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

    This will make the cron use the current display (Desktop).

    If you're running on Ubuntu this page might be of interest for you:
    https://help.ubuntu.com/community/CronHowto