I wonder if I can write a script that will monitor for a change in a file and execute some action when the change is detected.
Detailed explanation:
- OpenVPN writes its status to a file every 1 minute.
- I need to parse this status file and take action.
- OpenVPN truncates the status file before writing to it.
- I tried writing to a named pipe, but I get undesirable (but not fatal) errors in the app when it fails to truncate the pipe.
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:
Take a look at incron[1] or other inotify-stuff to trigger the execution of your script.
[1] http://inotify.aiken.cz/?section=incron&page=about&lang=en
inotify would be the right way to do it. Tutorials are given in some LinuxForYou magazine edition for this very question.
So have a script that:
1) Checks the modification time on the file (with stat)
2) If the modification time is newer than when last checked, it has changed
3) Parse it and perform your action
4) Else sleep for 1 minute and then reloop
Quick and dirty way :