2

How can I constantly monitor (small) file for changes?

Eg. when file is updated (action from web application), a script is executed (if not already running).

Right now, I do it every minute using cron, but this has delay up to one minute. I'd like to take the action immediately after the file is changed.

Maybe I need to write some low level process running in the background, once the server is started?

The reason I want to do it is to separate web application from root actions (performed on demand, once when file is updated).

3 Answers 3

3

incrond (inotify cron daemon) behaves like cron but uses inotify as the triggering mechanism. This guide has some pretty comprehensive examples.

An example incron entry for the case you described might be:

/path/to/file IN_CLOSE_WRITE /path/to/your_script
3
  • Thanks, this works nicely, except the fact I need an unique instance of /your_script to be running (ie. wait for the previous instance to finish)
    – takeshin
    Apr 7, 2015 at 14:02
  • @takeshin A cheap thing would be modify the script to sleep for a few seconds while an instance of itself already exists. Something like this: ubuntuforums.org/… Apr 7, 2015 at 14:34
  • The solution from your link works ok, thanks (but it assumes the .pid file exists and it's not empty)
    – takeshin
    Apr 7, 2015 at 17:01
1

incron is the software you're searching for.

0

If you are running Linux, as I assume you are, then you can rely on Linux inotify mechanism that notifies user-space processes when files change.

LSyncd is one program built around that feature, which you can configure to execute arbitrary commands when files change.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .