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

run.pl

use strict; use warnings; use IPC::Run qw(run); my($wtr, $rdr, $err); my @cmd = qw(script); my $pid = fork; my $timvariable = 7; unless($pid) { setpgrp; run \@cmd, '>>', "out.txt", '2>>', "err.txt"; } print "Child process id:$pid\n"; for(my $i =0;$i <= $timvariable ; $i++) { sleep(1); print "Parent is waiting to obtain the file which was created by c +hild process\n"; } kill 9,"-$pid"; kill 9,"$pid";

script

#!/usr/bin/perl use POSIX; while(1) { sleep(1); print "I am running\n"; my $pid = fork; print "Process group Id:",getpgrp $pid,"\n"; unless($pid) { while(1) { sleep(2); print "Process group Id in child:",getpgrp getpid(),"\n"; print "I am child and I'am running\n"; } } }

In the above code out.txt contains only the string that is printed by its child but it is not printing the strings that was printed by child of its child.

refer out.txt

I am running
Process group Id:15697
I am running
Process group Id:15697
I am running
Process group Id:15697
I am running

Here I want to print the characters "I am child and I'am running". How to solve this issue?. Thanks in advance

Replies are listed 'Best First'.
Re: Redirect the process group stdout to parent
by almut (Canon) on May 27, 2010 at 13:14 UTC

    Looks like you're Suffering from Buffering.  When I add $|=1; to your script, I get all the output (including from the grandchildren) in out.txt:

    I am running Process group Id:12773 Process group Id:12773 I am running Process group Id:12773 Process group Id:12773 I am running Process group Id:12773 Process group Id:12773 Process group Id in child:12773 I am child and I'am running I am running Process group Id in child:12773 I am child and I'am running Process group Id:12773 Process group Id:12773 Process group Id in child:12773 I am child and I'am running Process group Id in child:12773 I am child and I'am running I am running Process group Id:12773 Process group Id:12773 Process group Id in child:12773 I am child and I'am running Process group Id in child:12773 I am child and I'am running I am running Process group Id:12773 Process group Id:12773 Process group Id in child:12773 I am child and I'am running Process group Id in child:12773 I am child and I'am running Process group Id in child:12773 I am child and I'am running I am running Process group Id:12773 Process group Id:12773
      Ya it is working now. Thanks