Results 1 to 6 of 6
Hello all,
I have a project where I need to constantly monitor a log file. What I really need in my program is the same functionality as tail -f. This ...
- 05-22-2008 #1Just Joined!
- Join Date
- May 2008
- Location
- Sarasota, FL
- Posts
- 3
Tail -f like functionality
Hello all,
I have a project where I need to constantly monitor a log file. What I really need in my program is the same functionality as tail -f. This program needs to run as a daemon also. So my question is this, can I use tail -f and have the output sent to my daemon? If so how would I do this? If not, where can I find the code for the tail function so I can see how tail -f works?
Thanks,
Kevin
- 05-22-2008 #2
if you dont mind using Perl you can wrap tail -f and use it the way you want
Linux and me it's a love story
- 05-22-2008 #3Just Joined!
- Join Date
- May 2008
- Location
- Sarasota, FL
- Posts
- 3
I forgot to mention that I am a C/C++ programmer coming to Linux from Windows.
In answer to your reply, I would use Perl if I knew it. However because of time constraints I don't have the time to learn Perl well enough to do something like this.
Thanks,
Kevin
- 05-23-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Simple shell piping can help you here. But I can't be sure since I don't know what do you intend to do. A simple example:
Where <progname> will be the program that will be consuming the info that tail -f output (instead of sending it to stdout it will be piped towards this <progname>. A practical example would be:Code:tail -f foo.txt | <progname>
Which will pipe the output of tail -f into cat (yes, it's plainly useless but still illustrative). The lines will be sent to cat and cat will output them to stdout, just like tail -f alone would do. It's just an example. Change cat by any other thing and you have what you want.Code:tail -f foo | cat
- 05-23-2008 #5Just Joined!
- Join Date
- May 2008
- Location
- Sarasota, FL
- Posts
- 3
What we have is boxes that allow an analog phone to be used as a VoIP phone. The boxes check in with the server every so often. What I need to do is constantly monitor a log file for these "heartbeats" and store the last time we heard from each box. Then another Daemon will respond to our monitoring system when it requests an update on each box giving back information the first daemon has collected.
So what I was thinking, if I could get similar logic to tail -f into the first daemon it would be the most efficient way to handle this.
- 05-23-2008 #6
You want popen(3), with the second parameter set to "r". The default buffering for standard I/O in this case is fully buffered, and you probably don't want that. So call setbuf(3) or setvbuf(3) to make the I/O completely unbuffered, for timely results.
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote
