[Tutor] Desktop Notifications on Linux

Peter Otten __peter__ at web.de
Thu Apr 10 11:00:06 CEST 2014

Dharmit Shah wrote:

> I am trying to create a script that will go through the
> /var/log/secure file on a Linux system and provide desktop
> notifications for failed login attempts.
> Here is the code - http://pastebin.com/MXP8Yd91
> And here's notification.py - http://pastebin.com/BhsSTP6H
> I am facing issue in the function "new_attempts_from_last". I am not
> able to raise a desktop notification from this function. It always
> fails with this traceback - http://pastebin.com/cgHMScv3
> I see this error only when I try to raise a notification from the
> aforementioned function. If I run test examples under
> /usr/share/doc/notify-python/examples, it works just fine. Also, if I
> try to raise a notification from under if __name__ == "__main__":, it
> works without any issues. So I don't think there's any issue with OS's
> notification daemon. Running from python shell like below also works
> fine:
> In [1]: import notification
> In [2]: notification.notification("Hey")
> What am I missing or doing wrong here?
> If it matters, I am running Fedora 20, python 2.7 and Cinnamon desktop
> environment.
> For readability purposes, I have provided pastebin links. Let me know
> if this is not the correct way.

Maybe you are running the code as a user that has no "desktop"? Here's a 
strapped-down demo:

$ cat notify_demo.py 
import pynotify
import sys

n = pynotify.Notification("Notification", " ".join(sys.argv[1:]))
$ python notify_demo.py hello world

The notification appeared on my desktop. However, when trying again as root 
I got the error you are seeing:

$ sudo su
# python notify_demo.py hello again
Traceback (most recent call last):
  File "notify_demo.py", line 6, in <module>
glib.GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name 
org.freedesktop.Notifications was not provided by any .service files

More information about the Tutor mailing list