#!/usr/bin/perl -w use strict; $|++; use vars qw/ $opt_d $opt_v /; my $Version = '$Id: runmany,v 1.2 2002/12/21 06:42:42 felicity Stab $'; use Getopt::Std; &getopts("dv") || &Usage; &Usage if $#ARGV != 1; # sanity check on args present and accounted for my($proc,$limit) = splice @ARGV, 0, 2; print STDERR "Cmd: \"$proc\"\nNum: $limit\n" if $opt_v || $opt_d; my $started = 0; my $finished = 0; while (<>) { chomp; unless (fork()) { my $cmd; if ($proc =~ m/%s/) { ($cmd = $proc) =~ s/%s/$_/g; } else { $cmd = "${proc} $_"; } print STDERR "Running '$cmd'\n" if ( $opt_v || $opt_d ); exec $cmd; # we should never get here die "exec $cmd failed!: $!\n"; } ++$started; my $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] \n" . "\n" . "lines are read from stdin, and the values read are used to replace\n" . "any instances of \"%s\" in the . Copies of the modified\n" . "command are started until 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 \n" . "\n"; exit 1; }