This commit is contained in:
2020-03-26 21:08:51 +01:00
parent 6500a30df8
commit 8fe24f21bb
2 changed files with 11 additions and 10 deletions
-7
View File
@@ -1,8 +1,6 @@
"""Generic linux daemon base class for python 3.x.""" """Generic linux daemon base class for python 3.x."""
import sys, os, time, atexit, signal import sys, os, time, atexit, signal
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class Daemon: class Daemon:
"""A generic daemon class. """A generic daemon class.
@@ -56,11 +54,6 @@ class Daemon:
pid = str(os.getpid()) pid = str(os.getpid())
with open(self.pidfile,'w+') as f: with open(self.pidfile,'w+') as f:
f.write(pid + '\n') f.write(pid + '\n')
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='/usr/include', recursive=False)
observer.start()
def delpid(self): def delpid(self):
os.remove(self.pidfile) os.remove(self.pidfile)
+11 -3
View File
@@ -5,13 +5,21 @@ from watchdog.events import FileSystemEventHandler
class h2incDaemon(Daemon): class h2incDaemon(Daemon):
def run(self): def run(self):
while True: event_handler = MyHandler()
time.sleep(1) observer = Observer()
observer.schedule(event_handler, path='/usr/include', recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
class MyHandler(FileSystemEventHandler): class MyHandler(FileSystemEventHandler):
def on_modified(self, event): def on_modified(self, event):
print(f'event type: {event.event_type} path : {event.src_path}') print(f'event type: {event.event_type} path : {event.src_path}')
if __name__ == "__main__": if __name__ == "__main__":
daemon = h2incDaemon('/tmp/h2inc-daemon.pid') daemon = h2incDaemon('/tmp/h2inc-daemon.pid')
if len(sys.argv) == 2: if len(sys.argv) == 2: