# Begin ForkManager my $_max_procs = 5; $_pm = new Parallel::ForkManager($_max_procs); # Log at process fork $_pm ->run_on_start( sub { my ($pid, $host) = @_; print "Forking process PID: $pid\n"; } ); # Log at process copmletion $_pm ->run_on_finish( sub { my ($pid, $exit_code, $host) = @_; print "Finishing up process PID: $pid\n"; } ); $_pm->run_on_wait( sub { print "Waiting for children to finish.\n" }, 5.0 ); foreach my $ssh_host (keys %{$ssh_host_list}) { # Fork off the children and get going on the queries my $pid = $_pm->start($ssh_host) and next; # do script stuff here # Closing the forked process $_pm->finish; } # Ensure all children have finished $_pm->wait_all_children; exit(1);