python-daemon和看门狗的组合

时间:2015-07-03 09:26:19

标签: python daemon watchdog

我尝试运行以下代码段。一旦我守护应用程序,它就不再需要关注新的传入pcap文件了。

#!/usr/bin/env pypy

 import daemon
 import watchdog
 import logging
 import logging.handlers
 from logging import info, debug, warn, error
 from watchdog.observers import Observer
 from watchdog.events import PatternMatchingEventHandler
 from time import sleep

 def main():
      def on_created_pcap(event):
           error(event.src_path)

     handler = logging.handlers.SysLogHandler("/var/run/syslog")
     formatter = "%(filename)s: [%(levelname)s] %(message)s"
     handler.setFormatter(logging.Formatter(formatter))
     logging.getLogger().addHandler(handler)

     pattern = ['*.pcap']
     event_handler = watchdog.events.PatternMatchingEventHandler(pattern)
     event_handler.on_created = on_created_pcap

     observer = Observer()
     observer.schedule(event_handler, "./")
     observer.start()
     while True:
         sleep(1)

with daemon.DaemonContext():
     main()

没有它的守护进程上下文。有什么想法吗?

注意:我在OS X上运行代码片段。我在Linux机器上尝试它,你需要在SysLogHandler中替换/var/run/syslog w / ('localhost', 514)

0 个答案:

没有答案