By default, this seems to be set to around 5,000 to 8,000 on different machines I use, but is easy to increase. Because it uses inotify, it will barf on trying to watch directories containing more files than the max number of user inotify watches.It uses inotify, so won't work outside of Linuxland.Any events which then occur while COMMAND is executing are not ignored - they will cause a second dead period and subsequent execution. This is so that the flurry of events caused by the create-write-move dance which Vi or Emacs does when saving a buffer don't cause multiple laborious executions of a possibly slow-running test suite. On the first event, it enters a 'dead period' for 0.15 seconds, during which other events are ignored, before COMMAND is run exactly once.It doesn't lose events that happen while COMMAND is executing, unlike most of the other inotify solutions on this page.(Presumably this is less cool if you use another shell.) Because it's Bash, it interprets commands you pass it exactly as if you had typed them at a Bash prompt.It's extra super responsive, because it only calls inotifywait once, on startup, instead of running it, and incurring the expensive hit of establishing watches, on every iteration.Because it's so fast, you don't have to bother telling it to ignore large subdirs (like node_modules) just for performance reasons.Fabulous for running sub-millisecond unit tests, or rendering graphviz dot files, every time you hit 'save'. It uses inotify, so is more responsive than polling.It runs COMMAND every time there's a filesystem modify event within your current directory (recursive.) \Įxecute the github version as 'rerun' on your PATH, and invoke it using: rerun COMMAND ![]() Rerun2 ( on github) is a 10-line Bash script of the form: #!/usr/bin/env bashĮcho -quiet -recursive -monitor -event modify -format "%w%f". While read -r directory events filename doĪlternatively, use another tool that uses the same underlying functionality, such as incron (lets you register events when a file is modified) or fswatch (a tool that also works on many other Unix variants, using each variant's analog of Linux's inotify). To overcome this limitation, use inotifywait on the directory: inotifywait -e close_write,moved_to,create -m. If that's a problem, use the -format option to change the output to not include the file name: inotifywait -q -m -e close_write -format %e myfile.py |Įither way, there is a limitation: if some program replaces myfile.py with a different file, rather than writing to the existing myfile, inotifywait will die. However, beware that it assumes that the file name doesn't contain whitespace. The second snippet doesn't have this defect. The first snippet is simpler, but it has a significant downside: it will miss changes performed while inotifywait isn't running (in particular while myfile is running). Or inotifywait -q -m -e close_write myfile.py | Simple, using inotifywait (install your distribution's inotify-tools package): while inotifywait -e close_write myfile.py do. Since I need to accept only one, I'm accepting the one that I've actually used (it was simple, quick and easy-to-remember), even though I know it is not the most elegant. What's more, I want something to run in a terminal because I want to see the program output (I want to see error messages).Ībout the answers: Thanks for all your answers! All of them are very good, and each one takes a very different approach from the others. Update: I want something simple, discardable if possible. myfile.py doneīTW: I'm using Vim, and I know I can add an autocommand to run something on BufWrite, but this is not the kind of solution I want now. What I want is something like this: while sleep_until_file_has_changed myfile.py do. myfile.py doneĪnd then I need to go to that terminal and press Enter, whenever I save that file on my editor. ![]() I want something very simple, something I will leave running on a terminal and close it whenever I'm finished working with that file.Ĭurrently, I'm using this: while read do. ![]() I want a quick and simple way to execute a command whenever a file changes.
0 Comments
Leave a Reply. |