Answers to: Programming logic questionhttp://linuxexchange.org/questions/832/programming-logic-question<p>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.</p> <p>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...</p> <p>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?</p> <p>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?</p> <p>Thanks,</p> <p>-Rob</p>enWed, 06 Mar 2013 01:16:18 -0500Answer by darkonchttp://linuxexchange.org/questions/832/programming-logic-question/3066<p>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.</p>darkoncWed, 06 Mar 2013 01:16:18 -0500http://linuxexchange.org/questions/832/programming-logic-question/3066Answer by DBAhttp://linuxexchange.org/questions/832/programming-logic-question/1149<p>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.</p> <p>See man(1) tee or <a href="http://en.wikipedia.org/wiki/Tee_%28command%29" rel="nofollow">http://en.wikipedia.org/wiki/Tee_%28command%29</a></p> <p>Good Luck.</p>DBATue, 27 Jul 2010 01:47:12 -0400http://linuxexchange.org/questions/832/programming-logic-question/1149Answer by Web31337http://linuxexchange.org/questions/832/programming-logic-question/837<p>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 <strong>Sander Marechal</strong> shown, or you can code your own app using inotify functions on C or other lang providing inotify API(say, python).</p>Web31337Fri, 11 Jun 2010 12:38:31 -0400http://linuxexchange.org/questions/832/programming-logic-question/837Answer by Sander Marechalhttp://linuxexchange.org/questions/832/programming-logic-question/833<p>Make a filter and use tail. You could use the filter like so:</p> <pre><code>$ tail -f some.log | monitor </code></pre> <p>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.</p> <p>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.</p>Sander MarechalThu, 10 Jun 2010 22:28:49 -0400http://linuxexchange.org/questions/832/programming-logic-question/833