ホームプロジェクトにpython-daemonを使用しています。しかし、デーモンが 1 日の作業後に終了した理由がわかりません。
この私のコード:
import requests
import time
import subprocess
import os
import lockfile
import daemon
import logging
import signal
import sys
Class MyDaemon():
def run(self):
while True:
try:
self.check_updates()
time.sleep(5)
except ConnectionError as e:
self.log.error('Connerction Error')
time.sleep(120)
except (OSError, IOError) as e:
self.log.error('I/O Error')
time.sleep(120)
except Exception as e:
self.log.error('Exception: '.format(e.__class__))
time.sleep(120)
if __name__ == "__main__":
bot = MyDaemon()
context = daemon.DaemonContext(
working_directory=bot.path,
umask=0o002,
pidfile=lockfile.FileLock('/tmp/mydaemon.pid'),
)
# exit()
if context.pidfile.is_locked():
exit('daemon is running now!')
#context.signal_map = {signal.SIGHUP: 'terminate',
# signal.SIGUSR1: bot.reload_bot(),
# signal.SIGTERM: bot.exit_bot(),
# }
context.files_preserve = [bot.fh.stream] # logging
with context:
bot.run()
この電報専用ボット。関数 check_updates(self) では、リクエストを送信し、サーバーのレスポンスを処理します。これは、シェル (サーバー上の tmux) で正常に動作する関数です。関数 run() では、すべての例外をキャッチします (少なくとも私はそう思います:))。
ありがとう