in reply to Re: checking file everytime a new line is added to file
in thread checking file everytime a new line is added to file
turnstep's reply made me realize that the second half of the above proposal isn't portable. Recording the file size doesn't do you that much good except on Unix. If you start a script a while later and you want to read what lines were added since the last time that the script ran, then you need to know the seek offset to the previous end of the file; and this isn't the file size except on Unix (at least is isn't for DOS, Win32, OS/2, MacOS, nor VMS).
Another problem is to detect when the file has been replaced and you need to start from the beginning again. Checking for a changed i-node number (from stat) works pretty good, even on many non-Unix systems. But even under Unix, it misses some cases (and gives a false positive on other rare cases). Just noticing that the file got smaller is often an acceptable method. You can also cache the first few and/or last few lines and compare those quite quickly (or just remember the seek offset right before the last newline and check that the character at that offset is still a newline). Or combine all of these and reread the whole file if any of them fail, for example.
This type of approach can be quite nice if you don't need the lines processed immediately. It doesn't require one more process always hanging around. It is easy to "catch up" if something goes wrong.
- tye (but my friends call me "Tye")
|
|---|