Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I can't seem to get an exec call to print to a redefined STDOUT that is actually a pipe. Here's the code where I create the pipe and the child process


# dup stdout so we can restore it later
   if( !open( $old_stdout, ">&STDOUT" ) ) {
      return common::results->new( FAILURE, "Unable to dup STDOUT: $!" );
   }
   close( STDOUT );

   # create the piped stdout
   pipe( $smash_stdout, STDOUT );

   # set non blocking and unbuffered
   my $flags = fcntl( STDOUT, F_GETFL, 0 );
   fcntl( STDOUT, F_SETFL, $flags | O_NONBLOCK );
   select((select(STDOUT), $|=1)[0]);

   if( ($pid = fork()) ) {
      # parent process
      # reset out STDOUT
      if( !open( STDOUT, ">&", $old_stdout ) ) {
         return common::results->new( FAILURE, "Unable to dup STDOUT: $!" );
      }
      close( $old_stdout );
      $target->{stdout} = $smash_stdout;
      $target->{buffer} = "";
   } else {
      exec ($cmd);
      exit( -1 );
   }

And then later on in the parent thread, I run the following


      foreach $target (@{$self->{TARGETS}}) {
         if( defined( $target->{stdout} ) ) {
            my $smash_fh = $target->{stdout};
            $target->{buffer} .= <$smash_fh>;
         }
      }

If I print something to STDOUT before I exec, I read that through the pipe but the exec routine doesn't seem to send it through the pipe. I've deleted the code to redo stdout and the exec will print to the screen so the exec'ed program is printing, just not to my new STDOUT. Is there something different that I need to do?


In reply to Forks, Pipes and Exec by diabelek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-19 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found