Can a bash script be hooked to a file?
On linux, you can use the kernel's inotify
feature. Tools for scripting can be found there: inotify-tools.
Example use from wiki:
#!/bin/sh
EVENT=$(inotifywait --format '%e' ~/file1) # blocking without looping
[ $? != 0 ] && exit
[ "$EVENT" = "MODIFY" ] && echo 'file modified!'
[ "$EVENT" = "DELETE_SELF" ] && echo 'file deleted!'
# etc...
There is an API called inotify for C programmers.
There are some tools that use it, e.g. incron and inotify-tools.
Indeed there is: entr(1) will run arbitrary commands when files change, and also provides an auto-reload option for restarting application servers.
edit: some examples
Rebuild if sources files change
$ find *.c | entr make
Launch and auto-reload test server if files change
$ ls *.py | entr -r python main.py
Providing a the agument +/path/to/fifo
allows more complex scripting by instructing entr
to write the name of each file that changes to a named pipe. The following will convert Markdown files in the current directory to HTML as they're edited
$ ls *.md | entr +/tmp/notify &
$ while read F
> do
> markdown2html $F
> done < /tmp/notify