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

Hi all,

I need to start a postgres daemon and redirrect it's STDERR and STDOUT to a logfile rotator (can't mix them). Here's a sketch of what I'm thinking:

use IPC::Open3; our $rotatelogs_bin = ''; our $rotate_size = '10M'; # must end with M, see rotatelogs docs our $logdir = '/home/ahammond/logtest/log'; our $pg_bin = '/home/ahammond/logtest/belcher.pl'; our $pg_args = ''; # start by opening stdout and stderr loggers open (STDOUTLOG, "| $rotatelogs_bin $logdir/stdout.log $rotate_size") or die "Can't open stdoutlog: $!\n"; open (STDERRLOG, "| $rotatelogs_bin $logdir/stderr.log $rotate_size") or die "Can't open stderrlog: $!\n"; # filehandle manipulation here? our $pid = open3 (\*STDIN, \*STDOUTLOG, \*STDERRLOG, $pg_bin, $pg_args);
What I'm wondering is about efficiency. Is there a better way to do this? I'd very much like to exec the final call to the postmaster daemon so that I don't have the overhead of a perl instance. Is is possible to detach these processes? Should I be writing this in shell?

Drew

Replies are listed 'Best First'.
Re: efficiently dispatching loggers for daemon
by bageler (Hermit) on Mar 08, 2004 at 23:26 UTC
    make use of the system handles for in/out/err:

    somecommand > stdout.log 2>stderr.log

      Thanks for the comment, but this does not address my problem. I can't just dump output to a file. I need to feed it through a log rotator. And I need to keep STDERR and SDTOUT seperate so I can't do something like:

      exec 2>&1 daemon | rotatelogs &

      The daemon in question is Postgres' postmaster driving a pretty hefty database. I can't go restarting it every time I need to rotate a log file and it doesn't re-open log file handles on HUP.

      I'm considering re-dirrecting to named pipes, but I don't want to risk them getting full and blocking the database. Syslog sadly is not an option for political reasons.

      Any suggestions would be greatly appreciated.

        what's the difference in your stdout vs stderr? the standard method of starting postgres (using pg_ctl) already combines the outputs...

        I looked around and the only thing ppl have done to solve the problem of keeping them separate is to used named pipes. good luck!
Re: efficiently dispatching loggers for daemon
by bageler (Hermit) on Mar 23, 2004 at 23:46 UTC
    I was looking at the tcsh manual today and found something that may help!

    Diagnostic output may be directed through a pipe with the standard output. Simply use the form `|&' rather than just `|'.

    and

    The shell cannot presently redirect diagnostic output without also redirecting standard output, but `(command > output-file) >& error-file' is often an acceptable workaround. Either output-file or error-file may be `/dev/tty' to send output to the terminal.

    so it follows that you should be able to do `(command | stdoutlogger) |& errorlogger`