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

hi all,

I want use open3 function in perl daemon. Into the code i have closed STDOUT and open3 produce nothing. following is little script to reproduce my error

use strict; use IPC::Open3 ; use Data::Dumper ; my $auth ="admin1" ; my $pwd = "admin1" ; close(STDOUT) ; my $cmd = "net rpc rights list -U $authname%$pwd" ; my ( $wfh,$rfh,$efh,$pid,@privilege) ; $pid = open3( $wfh ,$rfh, $efh, $cmd ); open(FH,">/tmp/debug") ; waitpid( $pid, 0 ) ; if ( $? == 0 ) { print FH "Cmd OK\n" ; while(<$rfh>) { print FH $_ ; } } close(FH) ;

I don't understand why i can't use open3 when STDOUT is closed. I have tried to open annother file with :

open OUT, ">/tmp/out.tmp" or die "Can't open OUT" ; $pid = open3( $wfh ,\*OUT, $efh, $cmd ); ... ... while (<OUT>) { print FH $_ ; }

this produce the same problem

Thanks

Replies are listed 'Best First'.
Re: closed STDOUT and open3
by zentara (Cardinal) on Feb 04, 2010 at 12:20 UTC
Re: closed STDOUT and open3
by ikegami (Patriarch) on Feb 04, 2010 at 15:28 UTC
    Expect problems when you close STDOUT. It's safer to redirect it to /dev/null.

      Seconded.

Re: closed STDOUT and open3
by Anonymous Monk on Feb 04, 2010 at 12:23 UTC
    fork depends on STDIN/STDOUT/STDERR for inter-process communication, so does open3

      Neither fork nor open3 does any ipc*, so that's not true.

      open3 does setup the child's STD handles to do ipc (if so instructed), but that shouldn't depend on the parent's STD handles.

      I do consider this a bug in open3, but it's only triggered under odd, easily avoidable circumstances. ("It hurts when I do this." "So don't do that")

      * — Yet. At the moment, it's not possible to distinguish between an exec failure and the child exiting with code 255, and it's not possible to capture the cause of the exec failure. I have a patch for open3 (waiting on Perl 5.12's release) that will have open3 communicate exec failures to the parent via ipc.