Can a shell script wait for a file to change and take action?

Solution 1:

Cheap n' dirty way:

Loop stat -c %Y file and take action when the modification time changes.

Probably better:

Use the inotify cron service to watch for file modification events and run your action:

/path/to/your/file IN_MODIFY /path/to/your/script $#

Solution 2:

Take a look at incron[1] or other inotify-stuff to trigger the execution of your script.


Solution 3:

inotify would be the right way to do it. Tutorials are given in some LinuxForYou magazine edition for this very question.