このコードを使用してデーモン化する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 でウォッチドッグを記述して、他のデーモンを監視し、ダウンしたときに再起動する方法はありますか? (番犬を見守る者)