Search code examples
pythoncronpynotify

Python code working in a terminal but not in a cronjob


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

Solution

  • 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