1

I am trying to use python-daemon with this script to start and stop the run of some code in the background. (ultimate goal is to have this in an AWS instance).

For some reason that escapes me the pid file is not being generated and I think the process is not running.

Python script:

#standard python libs
import logging
import time

#third party libs
from daemon import runner

class App():

    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path = '/Users/NAME/Documents/workspace/RandomThings/testdaemon.pid'
        self.pidfile_timeout = 5

    def run(self):
        while True:
            #Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
            logger.debug("Debug message")
            logger.info("Info message")
            logger.warn("Warning message")
            logger.error("Error message")
            #Main code goes here ...
            sst=myClass()
            sst.run()
            time.sleep(10)

app = App()
logger = logging.getLogger("DaemonLog")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/Users/NAME/Documents/workspace/RandomThings/testdaemon.log")
handler.setFormatter(formatter)
logger.addHandler(handler)

daemon_runner = runner.DaemonRunner(app)
#This ensures that the logger file handle does not get closed during daemonization
daemon_runner.daemon_context.files_preserve=[handler.stream]
daemon_runner.do_action()

The in the shell (OSX) I do:

python daemon.py start >>>> this runs but the pid file is not created (tried sudo as well) python daemon.py stop >>>> returns error:

Traceback (most recent call last): File "SST_daemon.py", line 68, in daemon_runner.do_action() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py", line 189, in do_action func(self) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py", line 152, in _stop u"PID file %(pidfile_path)r not locked" % vars()) daemon.runner.DaemonRunnerStopFailureError: PID file '/Users/josefernandes/Documents/workspace/RandomThings/testdaemon.pid' not locked

I have tried to fix this for hours with not results so far.

Any reason why this is not working?

Any help is much much appreciated!!!!!

jpsfer
  • 594
  • 3
  • 7
  • 18

1 Answers1

2

Not sure why it didn't work for you, but maybe the code below might clear up a few things. I did have to improvise, as I didn't have myClass you mentioned. My Spam class has a run() method in it that loops: does yours? If you do, you might be surprised that your loop in App.run() doesn't loop since it stays in myClass. But that's an aside.

I had to make some path changes as I don't have OSX. But I tried not to change anything else and the code below works for me:

  1 #standard python libs
  2 import logging
  3 import time
  4 
  5 #third party libs
  6 from daemon import runner
  7 import spam.eggs
  8 
  9 class App():
 10 
 11     def __init__(self):
 12         self.stdin_path = '/dev/null'
 13         self.stdout_path = '/dev/tty'
 14         self.stderr_path = '/dev/tty'
 15         self.pidfile_path = '/home/fbicknel/tmp/testdaemon.pid'
 16         self.pidfile_timeout = 5
 17 
 18     def run(self):
 19         while True:
 20             #Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
 21             logger.debug("Debug message")
 22             logger.info("Info message")
 23             logger.warn("Warning message")
 24             logger.error("Error message")
 25             #Main code goes here ...
 26             # sst=myClass()
 27             # sst.run()
 28             sst=spam.eggs.Eggs()
 29             sst.run()
 30             time.sleep(10)
 31 
 32 app = App()
 33 logger = logging.getLogger("DaemonLog")
 34 logger.setLevel(logging.INFO)
 35 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
 36 handler = logging.FileHandler("/home/fbicknel/tmp/testdaemon.log")
 37 handler.setFormatter(formatter)
 38 logger.addHandler(handler)
 39 
 40 daemon_runner = runner.DaemonRunner(app)
 41 #This ensures that the logger file handle does not get closed during daemonization
 42 daemon_runner.daemon_context.files_preserve=[handler.stream]
 43 daemon_runner.do_action()

One thing I tried to get started was to eliminate myClass altogether and just let your loop in run() do the looping. That worked, so I added in spam.Eggs.

Hopefully this will get you going again.

Below is my module in spam/eggs.py:

  1 import time
  2 import logging
  3 
  4 class Eggs(object):
  5     def __init__(self, startvalue='green'):
  6         " init a spam object "
  7         self.logger = logging.getLogger("DaemonLog")
  8         self.color  = startvalue
  9 
 10     @property
 11     def color(self):
 12         return self._color
 13         
 14     @color.setter
 15     def color(self, value): 
 16         self._color = value
 17     
 18     def run(self):
 19         ' Just loop sounding happy. '
 20         while True:
 21             self.logger.info("yippie kai ai o")
 22             time.sleep(20)
 23             
 24 if __name__ == "__main__":
 25     spamalot = Eggs()
 26     print spamalot.color
 27     spamalot.color = 42
 28     print spamalot.color
fbicknel
  • 1,219
  • 13
  • 21
  • Thanks for this. Going through it now... I just tried a similar daemon with an 'Hello World' type class and it worked but this one continues to elude me. I am concerned this could be a numpy issue.... – jpsfer May 06 '14 at 20:13
  • Hey. This didn't work because I using a function in numpy(SVD) that apparently crashes when put in a separate process/worker. I ended up moving the project to linux machine in AWS and no issues there. – jpsfer Jun 01 '14 at 19:45