My immediate thought was to reach for Expect, which I use for managing process from Perl in other scripts.
Seems like a bit of overkill just to merge STDOUT, but it does work, since
(by default) the output of commands spawned by Expect goes to the script's STDOUT.
Here's a sample script, with debugging lines left in for flavor:
#!/usr/bin/perl -w
use strict;
use Expect;
$Expect::Multiline_Matching = 0;
my @todo = ("ls /etc", "ps axw", "du");
my @cmds;
foreach my $cmd (@todo) {
print "spawning $cmd\n";
push @cmds, Expect->spawn($cmd);
die "Didn't spawn $cmd\n" unless $cmds[-1];
}
print "cmds = @cmds\nExpecting...\n";
while (@cmds) {
expect(
1,
'-i', \@cmds,
[
qr"^.*\n",
sub {print "\n", shift, " did something\n"; exp_continue;}
],
[
'eof',
sub {
my $done = shift;
print "$done is finished\n";
@cmds = grep {$_ != $done} @cmds;
print "cmds = @cmds\n";
}
],
);
}
print "Everything finished\n";
The first pattern match is useless, it just adds some output to say which
command was active. The second match is the only required one for your purpose.
It deletes commands from the list as they finish, and recycles the expect call.
One benefit of going this route is that you could put in pattern
matches for rsh error output and take special action, while ignoring
most of the output.
Expect is nice for letting you take care of things that matter and ignore
the bulk of a process's output.
|