Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: @array_commands to execute simultaneously and collect output on single log

by choroba (Cardinal)
on Dec 15, 2016 at 14:47 UTC ( [id://1177847]=note: print w/replies, xml ) Need Help??


in reply to @array_commands to execute simultaneously and collect output on single log

I'm not sure about AIX, but the following worked for me on Linux, and it took less than 6 seconds:
#!/usr/bin/perl use warnings; use strict; my @commands = ( 'sleep 3 && find /usr/share/doc', 'sleep 4 && find /opt', 'sleep 5 && df' ); my @pids; for my $i (0 .. $#commands) { if (my $pid = fork) { push @pids, $pid; } elsif (defined $pid) { 0 == system "$commands[$i] > $i.o" or die "Can't run $commands +[$i]"; exit } else { die "Can't fork"; } } for my $i (0 .. $#commands) { print "=== $commands[$i] ===\n"; waitpid $pids[$i], 0; open my $fh, '<', "$i.o" or die "$i.o: $!"; print while <$fh>; unlink "$i.o"; }

The output from the first command goes first, etc.

Update

Or, without temporary files:

#!/usr/bin/perl use warnings; use strict; my @commands = ( 'sleep 3 && find /usr/share/doc', 'sleep 4 && find /opt', 'sleep 5 && df' ); my @fhs; for my $i (0 .. $#commands) { open $fhs[$i], '-|', $commands[$i] or die "Can't run $commands[$i] +"; } for my $i (0 .. $#commands) { print "=== $commands[$i] ===\n"; print while readline $fhs[$i]; }

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: @array_commands to execute simultaneously and collect output on single log
by nicopelle (Acolyte) on Dec 15, 2016 at 15:57 UTC
    Thanks !

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found