Listing 1 runmany.pl
#!/usr/bin/perl
$Version = 'runmany v1.5 - last change 2001-08-24 11:37:07-04';
use Getopt::Std;
&getopts("dv") || &Usage;
&Usage if $#ARGV != 1; # sanity check on args present and accounted for
$proc = shift(@ARGV);
$limit = shift(@ARGV);
print STDERR "Cmd: \"$proc\"\nNum: $limit\n" if $opt_v || $opt_d;
while (<>) {
chop;
unless (fork()) {
if ($proc =~ m/%s/) {
($cmd = $proc) =~ s/%s/$_/g;
} else {
$cmd = "${proc} $_";
}
exec $cmd;
}
++$started;
$running = $started - $finished;
print STDERR "$started $finished\n" if $opt_d;
if ($limit <= $running) {
wait;
++$finished;
}
}
# wait for last children and exit clean
until ($finished == $started) {
print STDERR "Final wait: $started $finished\n" if $opt_d;
wait;
++$finished;
}
exit 0;
sub Usage
{
print "\n$Version\n\n"
. "Usage:\n"
. " runmany [options] <cmd string> <count>\n"
. "\n"
. "lines are read from stdin, and the values read are used to replace\n"
. "any instances of \"%s\" in the <cmd string>. Copies of the modified\n"
. "command are started until <count> copies are running, then a new copy\n"
. "is started every time a command process ends.\n"
. "\n"
. "This is useful when more than one item may be processed at a time, but\n"
. "processing all possible commands at once could / would overload the system.\n"
. "\n"
. "Options:\n"
. " -v Verbose, output the command and count when starting\n"
. " -d Debug. Output a bunch of data while running.\n"
. "\n"
. "Example:\n"
. " ls | grep '\.o\$' | runmany \"gzip -8v %s\" 3\n"
. "\n"
. "would compress three files at a time (presumable on a system with at\n"
. "least that many processors.\n"
. "\n"
. "Author: Bill Davidsen <davidsen\@tmr.com>\n"
. "\n";
exit 1;
}
|