my %rc_hash = (); my $MAX_PROCESSES = 120; my $pm = new Parallel::ForkManager($MAX_PROCESSES); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; print "run_on_finish: $ident (pid: $pid) exited " . "with code: [$exit_code]\n"; $rc_hash{$pid} = $exit_code; } ); $pm->run_on_start( sub { my ($pid,$ident)=@_; print "** $ident started, pid: $pid\n"; } ); for(my $i=0; $i< @hosts; $i++){ $pm->start($hosts[$i]) and next; system(@some_command); $return_code = $? >> 8; $pm->finish($return_code); } foreach my $key (keys %rc_hash){ print "$key => $rc_hash{$key}\n"; } #### my $dbh = DBI->connect("DBI:mysql:database=mydb;host=$DB", "user", "pass", {'RaiseError' => 1}); my $MAX_PROCESSES = 120; my $pm = new Parallel::ForkManager($MAX_PROCESSES); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; print "run_on_finish: $ident (pid: $pid) exited " . "with +code: [$exit_code]\n"; insert_into_db(\$dbh, $pid, $exit_code); + } ); $pm->run_on_start( sub { my ($pid,$ident)=@_; print "** $ident started, pid: $pid\n"; } ); for(my $i=0; $i< @hosts; $i++){ $pm->start($hosts[$i]) and next; system(@some_command); $return_code = $? >> 8; $pm->finish($return_code); } $dbh->disconnect(); sub insert_into_db{ my $dbhdl = shift; my $pid = shift; my $ret_code = shift; $$dbhdl->do(INSERT INTO system_results ... .. ); }