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!!!!!