I'm looking for a high level view of how to get a program to tail a file continuously, not at intervals, and actually act on the data it has...whiles till continuously monitoring the file.
I'm running into a mental block trying to figure out how to keep the program monitoring, which a while(1=1) loop would certainly accomplish, but how do you act on the data you've gathered, while still monitoring the file...
If you exit the loop to act on the data, how do you still keep tabs on where you are in the file, it seems to me these need to be done in parallel...what am I missing?
If you need a more concise or clear answer please let me know, I'm having a hard time getting my mind wrapped around this. Thoughts?
Make a filter and use tail. You could use the filter like so:
Where monitor is your filter. In the monitor app, simply use fgets() to read from STDIN. fgets() reads from a handle and returns a complete line (ending in a new line). If there's no full ine yet, it simply waits. So, you call fgets(), wait for a line, act on it, then jump back to gfets() again.
This follows the UNIX philosophy. Do one thing and do it well. Use tail to follow the file. Use your monitor just to act on the lines.
answered 10 Jun '10, 22:28
there's "inotail" util can be used instead of "tail" if your kernel has inotify enabled(most default kernels from modern distros do). you can use it with the way Sander Marechal shown, or you can code your own app using inotify functions on C or other lang providing inotify API(say, python).
answered 11 Jun '10, 12:38
You might also find the command "tee" useful. It not only splits an output stream, but can write to any named pipe rather than just to a file. That would make real-time monitoring easier since you can parse the pipe by character if you wish.
See man(1) tee or http://en.wikipedia.org/wiki/Tee_%28command%29
answered 27 Jul '10, 01:47
One thing to be aware of is that -- if you're using fgets, you should call setlinebuf to set line-buffering so that you don't get stuck waiting for time-critical data to clear the buffers.
answered 06 Mar '13, 01:16