Signal processing in python-daemon

I installed python-daemon and now I'm trying to process the signal correctly. My code is:

 #!/usr/bin/env python # -*- coding: utf-8 -*- import signal, time, syslog import daemon def runDaemon(): context = daemon.DaemonContext() context.signal_map = { signal.SIGTERM: programCleanup } context.open() with context: doMainProgram() def doMainProgram(): while True: syslog.syslog("pythonDaemon is running") time.sleep(5) def programCleanup(): syslog.syslog("pythonDaemon STOP") if __name__ == "__main__": runDaemon() 

When I run the code, everything works as expected: the pythonDaemon text starts, writes to /var/log/syslog every 5 seconds. But when I want to end the daemon with kill -TERM *PID* , the daemon ends, but pythonDaemon STOP is missing from syslog.

What am I doing wrong?

NB: I do not work from from daemon import runner here because it gives me an error (it seems I need an older version of the lockfile ) and I will not fix it unless this is the only way to get the signal the correct call.

+8
python linux signal-handling python-daemon
source share
1 answer

Your code looks great, except that the signal handler is not called because it has the wrong signature. Do it like this:

 def programCleanup(signum, frame): 

Quoting documents ( signal.signal () ):

The handler is called with two arguments: the signal number and the current stack frame.

+8
source share

All Articles