open(MFOUT,">mfout.txt"); while () { print MFOUT $_; print $_; ... stuff that builds up commands in @commands... ... eg 'perl anotherscript.pl < input' } my %running = (); my $max_jobs = 200; my $job_count = 0; while ( @commands || %running ) { if ( @commands && ($job_count < $max_jobs)) { my $command = shift(@commands); my $pid; if ($pid = fork) { print "Parent Pid $$\n"; $running{$pid} = $command; ++$job_count; } else { die "cannot fork: $!" unless defined $pid; print "Child Pid $$ $command\n"; my $rc = system $command; exit($rc>>8); } } else { my $child_pid = wait(); if (! exists $running{$child_pid}) { warn "Reaped unkown process id $child_pid!!"; } elsif ($?) { my $rc = $? >> 8; warn "Process '$child_pid:$running{$child_pid}' errored with return code '$ rc'"; } print "command '$running{$child_pid}' completed\n"; delete $running{$child_pid}; --$job_count; } }