title: Daemon dies unexpectedly
body: <p>I have a python script, which I daemonise using this code</p>
  
  <pre><code>        def daemonise():
              from os import fork, setsid, umask, dup2
              from sys import stdin, stdout, stderr
  
              if fork(): exit(0)
              umask(0) 
              setsid() 
              if fork(): exit(0)
  
              stdout.flush()
              stderr.flush()
              si = file('/dev/null', 'r')
              so = file('daemon-%s.out'%os.getpid(), 'a+')
              se = file('daemon-%s.err'%os.getpid(), 'a+')
              dup2(si.fileno(), stdin.fileno())
              dup2(so.fileno(), stdout.fileno())
              dup2(se.fileno(), stderr.fileno())
              print 'this file has the output from daemon%s'%os.getpid()
              print &gt;&gt; stderr, 'this file has the errors from daemon%s'%os.getpid()
  </code></pre>
  
  <p>The script is in </p>
  
  <pre><code>while True: try: funny_code(); sleep(10); except:pass;
  </code></pre>
  
  <p>loop. It runs fine for a few hours and then dies unexpectedly. How do I go about debugging such demons, err daemons.</p>
  
  <p>[Edit]</p>
  
  <p>Without starting a process like monit, is there a way to write a watchdog in python, which can watch my other daemons and restart when they go down? (Who watches the watchdog.)</p>