Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Location
    Sarasota, FL
    Posts
    3

    Post 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

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    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

  3. #3
    Just 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

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by Bear34288 View Post
    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
    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:

    Code:
    tail -f foo.txt | <progname>
    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 | cat
    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.

  5. #5
    Just 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.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...