Daemon terminates unexpected

S

Stefan Neumann

I have written a daemon which should run endlessly. The structure looks
like this:

- start-stop-daemon forks my python program

then:

if __name__=="__main__":
try:
main()
except Exception,e
<write exception>

def main():
# I need a starter to use the program also from the unittests
starter=starter()
starter.start()

while True:
#TODO: Catch kill signal
# I know, dirty....
sleep(10)

class Starter(Thread):

def __init__(<not interesting>):
Thread.__init__(self)
<do some init stuff>
self.setDaemon(True)

def run(self):
try:
<start an SMTP-Server
asyncore.loop()
except Exception,e:
<write exception in log>

The problem is now that the SMTP-Server quits donig its work. First I
thought, there will be an exception be thrown but actually, after
catching all global exceptions there is no log entry for it.

I really don't know why my program terminates. It happens nearly
regularly after five days.

Why I think, everything works correctly (correct me, if I am wrong):

Main will actually live forever because of the endless loop.
It starts a thread, which is set as daemon. This daemon will live until
all non daemon threads have terminated. So why does this program end?

I have read somethong about double-fork, can someone explain, why to use
this way for starting a daemon?

Thanks in advanced.

Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBD5xp7/gltz1ptagYRAmzSAJ4piq2RvFQaU9uftcHBO+qYeRXzIgCghNVj
UuCIwcujvs/IdrG9n4ma9pE=
=XO1b
-----END PGP SIGNATURE-----
 
S

Steve Horsley

Incorporating Fredrik's fix (I learned something new reading
that), try using an endless loop even if there is an exception
like this:

def run(self):
while True:
try:
<start an SMTP-Server
asyncore.loop()
except:
e = sys.exc_value()
<write exception in log>
<kill the SMTP server off>
time.sleep(60) # wait, avoid spinning

so that even if there is an exception, it'll get up and go again.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top