Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Signal handling in python-daemon

I installed python-daemon and now I'm trying to get the signal handling right. My code:

#!/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 start the code everything works as expected: The text pythonDaemon is running gets written to /var/log/syslog every 5 seconds. But when I want to terminate the daemon with kill -TERM *PID* the daemon is terminated but the text pythonDaemon STOP is missing from syslog.

What am I doing wrong?

NB: I am not working with from daemon import runner here, cause that gives me an error (looks like I need an older version of lockfile) and I will not fix this unless it is the only possibilty to get the signal-handling right.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

def programCleanup(signum, frame):

Quoting the docs (signal.signal()):

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!