#!/usr/bin/env perl use strict; use warnings; use Time::HiRes qw(sleep); my $workercount = 0; # Counts active workers SIG{CHLD} = sub { # Child exit detected $workercount--; # remove child from "active" count return; } while(1) { if($workercount < 20) { my $pid = fork(); if($pid) { # parent $workercount++; # forked a new child } else { # child my $cmd = 'perl myworkerscript.pl'; eval { exec($cmd); }; exit(0); } } else { sleep(0.5); } }