I am trying to learn python-watchdog, but I am a little confused why the work that I configured runs several times. So here is my setup:
import os
from watchdog.events import FileSystemEventHandler
from actions import run_something
def getext(filename):
return os.path.splitext(filename)[-1].lower()
class ChangeHandler(FileSystemEventHandler):
def on_any_event(self, event):
if event.is_directory:
return
if getext(event.src_path) == '.done':
run_something()
else:
print "event not directory.. exiting..."
pass
The observer is configured as follows:
import os
import time
from watchdog.observers import Observer
from handler import ChangeHandler
BASEDIR = "/path/to/some/directory/bin"
def main():
while 1:
event_handler = ChangeHandler()
observer = Observer()
observer.schedule(event_handler, BASEDIR, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
main()
and finally actions like this:
import os
import subprocess
def run_something():
output = subprocess.check_output(['./run.sh'])
print output
return None
.. where ./run.shis just a shell script I would like to run when the file with the extension .doneis on/path/to/some/directory/bin
#!/bin/bash
echo "Job Start: $(date)"
rm -rf /path/to/some/directory/bin/job.done
echo "Job Done: $(date)"
However, when I issue python observer.pyand then do touch job.doneon /path/to/some/directory/bin, I see that my shell script ./run.shis executed three times, not one.
I am confused why this is done three times and not just once (I delete the file job.donein my bash script)
Johnj source
share