Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8348377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:40:16+00:00 2026-06-09T07:40:16+00:00

Basic need : I’ve a Python daemon that’s calling another program through os.system. My

  • 0

Basic need : I’ve a Python daemon that’s calling another program through os.system. My wish is to be able to properly to handle system shutdown or SIGTERM in order to let the called program return and then exiting.

What I’ve already tried: I’ve tried an approach using signal :

import signal, time

def handler(signum = None, frame = None):
    print 'Signal handler called with signal', signum
    time.sleep(3)
    #here check if process is done
    print 'Wait done'

signal.signal(signal.SIGTERM , handler)

while True:
    time.sleep(6)

The usage of time.sleep doesn’t seems to work and the second print is never called.

I’ve read few words about atexit.register(handler) instead of signal.signal(signal.SIGTERM, handler) but nothing is called on kill.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:40:17+00:00Added an answer on June 9, 2026 at 7:40 am

    Your code does almost work, except you forgot to exit after cleaning up.

    We often need to catch various other signals such as INT, HUP and QUIT, but not so much with daemons.

    import sys, signal, time
    
    def handler(signum = None, frame = None):
        print 'Signal handler called with signal', signum
        time.sleep(1)  #here check if process is done
        print 'Wait done'
        sys.exit(0)
    
    for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
        signal.signal(sig, handler)
    
    while True:
        time.sleep(6)
    

    On many systems, ordinary processes don’t have much time to clean up during shutdown. To be safe, you could write an init.d script to stop your daemon and wait for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate a basic authorization header that is being sent to my
I need a basic JSON parser that works with J2ME / CLDC 1.1. A
I need a basic regular expression for validating a url, such that: Both http://
basic problem: i need to create real customized templates for my django forms. That's
please help with some basic syntax i need to make a statement that checks
I need a basic pdf generator that'll let me toss some images and text
I need a basic example of how to use the IComparable interface so that
I have a set of shapes created in visual basic that need to be
I need very basic template engine, which is incapsulated in single class. So I
I find the documents frustrating. What are the basic lines I need to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.