in reply to Re^2: Daemon, log4perl & stderr/stdout
in thread Daemon, log4perl & stderr/stdout

You need to make sure fileno(STDERR) is equal to 2.

fileno()==0 will be used as the child's STDIN.
fileno()==1 will be used as the child's STDOUT.
fileno()==2 will be used as the child's STDERR.

The above will allow you to redirect STDERR to a file, but system file handles (as opposed to Perl file handles) can't be redirected to a Perl function (such as the log4perl handler). If you wish to do that, you'll have to use a temporary file, IPC::Open3 or something like pc88mxer's code.

Replies are listed 'Best First'.
Re^4: Daemon, log4perl & stderr/stdout
by pc88mxer (Vicar) on May 27, 2008 at 23:17 UTC
    This is a good point. If you don't explicitly close(STDERR), it should stay at file descriptor 2 when you re-open it. For example:
    close(STDIN); close(STDERR); open(STDERR, ">/tmp/foo"); print fileno(STDERR), "\n"; # prints 0
    However, if the close(STDERR) call is removed, STDERR remains at file descriptor 2.

    A quick test of Proc::Daemon indicates that STDIN, STDOUT and STDERR all have the expected file descriptors.