Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Daemon, log4perl & stderr/stdout

by saberworks (Curate)
on May 27, 2008 at 22:25 UTC ( [id://688759]=note: print w/replies, xml ) Need Help??


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

Thanks for your reply. I guess I should be clear, when I'm running "child" processes I'm using system or backticks, not actually opening child processes. I guess I could do what you mention, although really these commands need to run in sequence, not parallel, so I'm not sure forking a child of the daemon manually like that is necessary.

Also, about Proc::Daemon redirecting STDOUT and STDERR to /dev/null, is there a way to re-redirect them? When I don't run in "daemon" mode (I comment out Proc::Daemon::Init()), I can do this:
open STDERR, '>', "$run_path/stderr.txt" or die "Can't redirect STDERR: $!";
And the STDERR output from each command subsequently run using backticks `/some/command` gets written to the stderr.txt log file. But if I run in Daemon mode (uncomment the Proc::Daemon::Init() call), the STDERR & STDOUT info is getting lost. Even though I'm opening them as above. I wonder if I need to close them first. (update closing them first makes no difference, it's as if once Proc::Daemon gets ahold of them I can't redirect them elsewhere)

Replies are listed 'Best First'.
Re^3: Daemon, log4perl & stderr/stdout
by ikegami (Patriarch) on May 27, 2008 at 22:46 UTC

    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.

      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.

Re^3: Daemon, log4perl & stderr/stdout
by pc88mxer (Vicar) on May 27, 2008 at 22:37 UTC
    You'll be running the children in sequence because the parent will be capturing the output from each child until the child terminates before spawning the next child.

    As for STDOUT and STDERR when using Proc::Daemon, it just redirects them to /dev/null as a convenience. There's no problem re-opening them to other files. Closing those handles first is not necessary. When attempting to open an already open file handle, perl will close it first.

    If the open STDERR ... call is not working in daemon mode, perhaps it's because it can't write to or create "$run_path/stderr.txt". What are the permissions on the directory and file, and what userid is the script running as in daemon mode?

      Great, I misunderstood your code at first, after looking through the manual and your explanation it now makes sense. I'm going to implement it like this and I'll report back the status. Thanks for your time & help.
Re^3: Daemon, log4perl & stderr/stdout
by ikegami (Patriarch) on May 27, 2008 at 22:59 UTC
    Works for me
    #!/usr/bin/perl use Proc::Daemon qw( ); use File::Basename qw( dirname ); use File::Spec::Functions qw( rel2abs ); my $homedir; BEGIN { $homedir = dirname(rel2abs($0)); } { Proc::Daemon::Init; chdir($homedir); open STDERR, '>', 'stderr.txt'; system(q{perl -e'print STDERR "mwuhahahaha\n"'}); }
    $ 688767.pl $ cat stderr.txt mwuhahahaha
    Could you show us what doesn't work?
      Actually, this works perfectly. For some reason it wasn't working yesterday but it is working today with no code changes. I must have been running an older version or something.

      This is great to just redirect stderr & stdout to individual logs. I'm going to explore the first answer to my question on getting this working with log4perl.

      Thanks for your time & replies.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://688759]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-16 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found