3

I've written a code that'll notify me whenever it is executed. I want to run this code after every half an hour, hence created a cronjob. But the code does not work when executed through cronjob.

Here's my code:

import sys
import pynotify

if __name__ == "__main__":
    if not pynotify.init("icon-summary-body"):
        sys.exit(1)
    n = pynotify.Notification("Subject","Message","notification-message-im")
    n.show() #throws error here

Cronjob:

* * * * * cd /home/username/Documents && /usr/bin/python file.py >> /home/username/Desktop/mylog.log 2>&1

Cronjob log:

/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
  warnings.warn(str(e), _gtk.Warning)
Traceback (most recent call last):
  File "file.py", line 8, in <module>
    n.show()
gio.Error: Cannot autolaunch D-Bus without X11 $DISPLAY

What could be the possible reason of the same?

I tried using notify2 as well, but to no success. Works in normal execution but not via cronjob

Here's the notify2 code:

import notify2
notify2.init('app name')
n = notify2.Notification("Summary","Some body text","notification-message-im")
n.show()

Cronjob logs for notify2 script:

Traceback (most recent call last):
  File "file.py", line 3, in <module>
    notify2.init('app name')
  File "/usr/local/lib/python2.7/dist-packages/notify2.py", line 93, in init
    bus = dbus.SessionBus(mainloop=mainloop)
  File "/usr/lib/python2.7/dist-packages/dbus/_dbus.py", line 211, in __new__
    mainloop=mainloop)
  File "/usr/lib/python2.7/dist-packages/dbus/_dbus.py", line 100, in __new__
    bus = BusConnection.__new__(subclass, bus_type, mainloop=mainloop)
  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 122, in __new__
    bus = cls._new_for_bus(address_or_type, mainloop=mainloop)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

How can I run this code via a cronjob?

EDIT

With the following code :-

import sys
import pynotify
import os

os.environ.setdefault('XAUTHORITY', '/home/user/.Xauthority')
os.environ.setdefault('DISPLAY', ':0.0')

if __name__ == "__main__":
    if not pynotify.init("icon-summary-body"):
        sys.exit(1)
    n = pynotify.Notification("Subject","Message","notification-message-im")
    n.show() #throws error here

I'm now getting :-

/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
  warnings.warn(str(e), _gtk.Warning)
Traceback (most recent call last):
  File "file.py", line 12, in <module>
    n.show() #throws error here
gio.Error: Could not connect: Connection refused
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267

2 Answers2

3

Did you try to call the display in your cronjob ?

Cronjob:

* * * * * DISPLAY=:0.0 python /home/username/Documents/file.py

In your python code you can also try to call the Display, at the beginning:

import os
# environnement vars
os.environ.setdefault('XAUTHORITY', '/home/user/.Xauthority')
os.environ.setdefault('DISPLAY', ':0.0')

Also, pynotify don't works in root. So you should write your crontab without "sudo"

crontab -e
Guillaume
  • 2,752
  • 5
  • 27
  • 42
  • you can try also to call the display in your python code, I've edited my answer. – Guillaume Oct 14 '14 at 07:16
  • Do you call pynotify in root or as a normal user? -> How do you modify your conjob: `contab -e` or `sudo crontab -e` ? – Guillaume Oct 14 '14 at 07:30
  • I'm calling it as a normal user. I modify my crontab without `sudo` – Praful Bagai Oct 14 '14 at 07:31
  • Using `sudo crontab -e` works for me. Can you please explain why is it like that? – Praful Bagai Oct 14 '14 at 07:34
  • 1
    Ok. So if you modify your crontab with sudo, the script will be executed as root. And pynotify is buging in root.. So, you should try to remove your sudo crontab, and re-call your script using `contab -e`, without sudo – Guillaume Oct 14 '14 at 07:35
  • I mean to say it works while editing `sudo crontab -e` and did not worked when editing only `crontab -e`. – Praful Bagai Oct 14 '14 at 07:41
  • It's a bug in pynotify, notify-osd etc, when called in root.. I had the same error in one of my script! I'm happy that it's working now :) – Guillaume Oct 14 '14 at 07:44
0

Try setting the environment variable $DISPLAY

* * * * * env DISPLAY=:0 cd /home/username/Documents && /usr/bin/python file.py >> /home/username/Desktop/mylog.log 2>&1
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52