in reply to Pipe, fork, exec and red-hot pokers.
When STDOUT isn't a tty, it turns off line buffering. Hence my requirement to snarf STDOUT in real time was thwarted, until I figured the following out.
This code snipped forks and execs an application, using select to wait for available output on the stderr and stdout for the exec()ed application. The streams are not interleaved.
--use IO::Pty; use IO::Select; my $pty = new IO::Pty; pipe($readerr, $writeerr); if (my $pid = fork) { close($writeerr); my $select = new IO::Select; $select->add($pty); $select->add($readerr); while (1) { foreach my $fh ($select->can_read) { my $buf; if (sysread($fh, $buf, 4096)) { print "Read ... $buf ...\n"; } } } } else { close($readerr); $pty->make_slave_controlling_terminal(); my $slave = $pty->slave(); close $pty; $slave->clone_winsize_from(\*STDIN); $slave->set_raw(); open(STDOUT, ">&" . $slave->fileno); open(STDERR, ">&" . $writeerr->fileno); close($slave); exec("/home/hagus/foo.pl"); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Further information
by hagus (Monk) on Apr 09, 2002 at 11:46 UTC |