I can't seem to redirect STDOUT to a file before forking in my perl
program.
In the parent, I want to redirect STDOUT to 'logfile' then fork. In the
child, I want to print something. That something should end up in the
log file, but it's not working.
Here is my code:
use strict;
use warnings;
use constant EXIT_OK => 0;
$SIG{CHLD} = "IGNORE";
my $child = fork();
exit ($child ? parent() : child() );
sub parent {
close STDOUT;
open (STDOUT, '>', 'logfile') or die("Couldn't redirect STDOUT: $!\n");
EXIT_OK;
}
sub child {
print "Child's pid = $$.\n";
EXIT_OK;
}
__END__
This is my catch-22. I need to setup the redirection before I fork, but
I shouldn't redirect from within the child, and I can't find out if I am
the child or not until I've called fork :(
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>