Skip to main content
2 of 3
added 611 characters in body
Michael Mrozek
  • 95.7k
  • 40
  • 245
  • 236

According to Gilles on Super User:

Simple, using inotifywait (install your distribution's inotify-tools package):

while inotifywait -e close_write myfile.py; do ./myfile.py; done

This has a big limitation: if some program replaces myfile.py with a different file, rather than writing to the existing myfile, inotifywait will die. Most editors work that way.

To overcome this limitation, use inotifywait on the directory:

while true; do
  change=$(inotifywait -e close_write,moved_to,create .)
  change=${change#./ * }
  if [ "$change" = "myfile.py" ]; then ./myfile.py; fi
done
Tobias
  • 577
  • 3
  • 10