You want to be able to:
- Run several external commands concurrently.
- Check the output from those commands for some keyword(s).
- (Optionally?) Have the outputs end up in files. (or is this just so you can search the output later?).
- Have the script that starts the concurrent processes know:
- when the tasks completed.
- Whether the output contained the keyword(s).
threads and the 'piped' version of open (see perlopentut) makes this kind of concurrency easy.
#! perl -slw
use strict;
use threads qw[ async ];
use threads::shared;
sub runAndCheck {
my( $cmd, $lookFor, $file, $checkRef, $doneRef ) = @_;
open OUT, '>', $file or die "$file : $!";
open CMD, "$cmd |" or die "$cmd : $!";
while ( <CMD> ) {
$$checkRef = 1 if $_ =~ $lookFor;
print OUT;
}
close CMD;
close OUT;
$$doneRef = 1;
}
my @cmds = map{ "dir $_" } qw[ c:\ P:\test r:\ d:\ ];
my @checks : shared = ( 0 ) x @cmds;
my @dones : shared = ( 0 ) x @cmds;
my $lookFor = 'junk.htm';
async{
runAndCheck(
$cmds[ $_ ],
$lookFor,
"test$_.out",
\$checks[ $_ ],
\$dones[ $_ ]
);
}->detach for 0 .. $#cmds;
while( grep( $_, @dones ) < @cmds ) {
sleep 1;
for ( 0 .. $#cmds ) {
if( $dones[ $_ ] == 1 ) {
printf "'$cmds[ $_ ]' completed; '$lookFor' ";
print $checks[ $_ ] ? 'was found' : 'was not found';
$dones[ $_ ] = 2;
}
}
}
__END__
P:\test>412014
'dir c:\' completed; 'junk.htm' was not found
'dir P:\test' completed; 'junk.htm' was found
'dir r:\' completed; 'junk.htm' was not found
'dir d:\' completed; 'junk.htm' was not found
P:\test>412014
'dir c:\' completed; 'junk.htm' was not found
'dir r:\' completed; 'junk.htm' was not found
'dir d:\' completed; 'junk.htm' was not found
'dir P:\/s' completed; 'junk.htm' was found
Examine what is said, not who speaks.
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail
"Time is a poor substitute for thought"--theorbtwo
"Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
-
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.
|