Tom Phoenix wrote:
On 6/30/06, Mumia W. <[EMAIL PROTECTED]> wrote:

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.

my $child = fork();
exit ($child ? parent() : child() );

It's good to check whether the fork returned undef due to a failed fork.


Okay, I'll try to remember that.

sub parent {
     close STDOUT;
open (STDOUT, '>', 'logfile') or die("Couldn't redirect STDOUT: $!\n");

If this happens only in the parent, it won't affect the child, as you've found.


Yes, I've learned that the system command has special magic that allow
the child to inherit the parent's file descriptors, but fork doesn't do
this.

I need to setup the redirection before I fork, but
I shouldn't redirect from within the child,

Why don't you set up the redirection after the fork? Why shouldn't you
do the redirection from the child process? That's how I always do it.
[...]

Yes, with fork, I'll have to do that. I wanted to mimic system's behavior using fork, but I've concluded that that's probably too complicated for me.

Thanks Tom.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to