我有一个 python 脚本,我使用这段代码来守护它
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 >> stderr, 'this file has the errors from daemon%s'%os.getpid()
脚本在
while True: try: funny_code(); sleep(10); except:pass;
环形。它运行了几个小时,然后意外死亡。我该如何调试这样的恶魔,错误的守护进程。
[编辑]
在不启动monit之类的进程的情况下,有没有办法在python中编写一个看门狗,它可以监视我的其他守护进程并在它们出现故障时重新启动?(谁看门狗。)