My understanding of Python's daemon module is that I can have a script that does stuff, spawns a daemon, and continues to do stuff. When the script finishes the daemon should hang around. Yes?
But that's not happening...
I have a python script that uses curses to manage a whole bunch of scripts and functions. It works wonderfully except when I use the script to spawn a daemon.
Now a daemon in this application is represented by a class. For example:
class TestDaemon(DaemonBase):
def __init__(self,stuff):
logger.debug("TestDaemon.__init__()")
def run(self):
logger.debug("TestDaemon entering loop")
while True:
pass
def cleanup(self):
super(TestDaemon,self).cleanup()
logger.debug("TestDaemon.cleanup()")
def load_config(self):
super(TestDaemon,self).load_config()
logger.debug("TestDaemon.load_config()")
And the daemon is launched with a function like:
def launch(*args,**kwargs):
import daemon
import lockfile
import signal
import os
oDaemon = TestDaemon(stuff)
context = daemon.DaemonContext(
working_directory=os.path.join(os.getcwd(),sAppName),
umask=0o077, #chmod mode = 777 minus umask. Only current user has access
pidfile=lockfile.FileLock('/home/sheena/.daemons/{0}__{1}.pid'.format(sAppName,sProcessName)),
)
context.signal_map = {
signal.SIGTERM: oDaemon.cleanup, #cleanup
signal.SIGHUP: 'terminate',
signal.SIGUSR1: oDaemon.load_config, #reload config
}
logger.debug("launching daemon")
with context:
oDaemon.run()
logger.debug("daemon launched")
The program gets as far as logging "launching daemon".
After this point, everything exits and the daemon doesn't run.
Any ideas why this would happen?
There is no evidence of exceptions - exceptions are set to be logged but there are none.
Question: Any ideas why this could be happening?
Stuff I've tried:
- If I have
oDaemon.run()
in a try block it fails in exactly the same way - I assumed maybe the context is set up wrong so replaced
with context
withwith daemon.DaemonContext()
. Same problem I replaced:
with context: oDaemon.run()
with
def run():
while True:
pass
with context:
run()
and the main program still exited prematurely but at least it spawned a daemon so I assume it doesn't like the way I put stuff in a class...