According to GillesGilles on Super UserSuper User:
Simple, using inotifywait (install your distribution's
inotify-toolspackage):
while inotifywait -e close_write myfile.py; do ./myfile.py; done
This has a big limitation: if some program replaces
myfile.pywith a different file, rather than writing to the existingmyfile,inotifywaitwill die. Most editors work that way.
To overcome this limitation, use
inotifywaiton the directory:
while true; do
  change=$(inotifywait -e close_write,moved_to,create .)
  change=${change#./ * }
  if [ "$change" = "myfile.py" ]; then ./myfile.py; fi
done