Kolyan has asked for the wisdom of the Perl Monks concerning the following question:

Hello, dear monks!

I want to store net-acct's logs in the PostgreSQL database.
Program itself doesn't allow this, so I used File::Tail to consequentally read it's logfile.
But now I have another idea, e.g. to substitute program's logfile with fifo and read the data from it.
I tested it with the following (very simple) script:

#! /usr/bin/perl -w use strict; my $fifo = "/var/log/net-acct/net-acct.log"; while (1) { open(FIFO, "< $fifo") or die "Can't open fifo $fifo: $!\n"; while (<FIFO>) { print; } close(FIFO) or die "Can't close fifo $fifo: $!\n"; }

Everything seems to work just fine, so I want to ask you is there are things I should be aware of then working with named pipes?
Or even more generally is it a good idea at all?

Replies are listed 'Best First'.
Re: named pipe question
by jeffenstein (Hermit) on Apr 17, 2002 at 09:39 UTC

    If your process is not running, or dies for some reason, the process writing to this log file will eventually block. This will probably cause the entire process to freeze, as writing to a log file is something that would be expected to return immediately.

    Also, if the process writing to the file happens to rotate log files, your fifo will stop recieving data. File::Tail will open the new file and continue happily reading the data at the end of the file.

Re: named pipe question
by broquaint (Abbot) on Apr 17, 2002 at 09:45 UTC
    are there things I should be aware of then working with named pipes?
    • When you write to a named pipe, there must be something reading from the other end or the write will fail.
    • You can't loop on a pipe like you might on a file, as after the first read (unless something has already been written (not likely)) you'll get an EOF and the loop will terminate.

    The code you've provided should cover the above caveats though, so you should be fine. This node also has some good info on named pipes (and seems to cover a situation similiar to your own).
    HTH

    broquaint

Re: named pipe question
by mce (Curate) on Apr 17, 2002 at 09:46 UTC
    Hi,

    Named Pipes are an excellent idea when talking to strangers . And since it is mentioned in the Camel Book (page 345), you are certainly not the only one who uses this procedure.

    But, I would include an retry mechanism because I think that when net-acct closes it's filehandle, the <> is terminated.

    And there are better ways of setting up a daemon than with while (1) , for example Proc::Daemon.

    Yours,
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium