Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

  1. OpenVPN writes its status to a file every 1 minute.
  2. I need to parse this status file and take action.
  3. OpenVPN truncates the status file before writing to it.
  4. 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.
share|improve this question
    
In case the modification is actually appending to the file, hanging on tail -f is probably the easiest way. But only for appending. –  SF. Jul 5 '11 at 14:35

4 Answers 4

up vote 17 down vote accepted

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 $#
share|improve this answer
    
+1 for the second solution –  Michael Lowman Jul 5 '11 at 17:19

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.