Daiver
Daiver

Reputation: 1508

pynotify doesn't work with daemon

I am trying to create small notify daemon

pynotify works in main thread But pynotify does not work in daemon run-cycle. After show method program just waits something I took daemon sample here: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

My daemon:

import pynotify
import sys, time
from daemon import Daemon
class NotifyDaemon(Daemon):
    def run(self):
        i = 9
        while True:
            pynotify.init('icon-summary-body')
            n = pynotify.Notification('Test', 'text ')
            print 'Test from stdout'# OK but only one message
            n.show() # not OK
            time.sleep(1)
if __name__ == "__main__":
    if not pynotify.init('icon-summary-body'):
        print 'PyNotify init failed!'
        exit(2)
    daemon = NotifyDaemon('/tmp/reminderdaemon.pid', stdout='/dev/stdout')
    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            n = pynotify.Notification('Daemon starts!')#this message is ok
            n.show()# OK
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
        sys.exit(2)

OS:Ubuntu WM: Awesome

Is there a way out? PS sorry for my writing mistakes. English in not my native language

Upvotes: 1

Views: 475

Answers (1)

aychedee
aychedee

Reputation: 25639

You can ditch the daemonization code. If you want a process that runs on boot then use upstart. All you need to do is place a file in /etc/init/my_server.conf that contains something like this:

description "My Server"
author "Dark Daiver [email protected]"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

exec python /home/dd/my_server.py 

That will do a respawn if the process dies as well.

So if all you had in your my_server.py file was:

import pynotify


if __name__ == "__main__":

   pynotify.init('icon-summary-body')
   n = pynotify.Notification('Test', 'text ')

   while True:
        n.show() # not OK
        time.sleep(1)

That would start on boot and show a notification once a second.

Upvotes: 2

Related Questions