use strict; use warnings; use POSIX ":sys_wait_h"; handler(); sub handler { my %process = ( A => 'sleep 10 && exit 0', B => 'sleep 15 && exit 0', C => 'sleep 25 && exit 1', ); my @child_pids; foreach my $process(keys %process) { my $pid = fork; if($pid) { #Parent. # print "Launched $process | $pid\n"; push(@child_pids, {PID => $pid, PROCESS => $process{$process}}); } elsif($pid == 0) { exec $process{$process}; die "Can't exec $process"; } else { #Parent # Fork failed. die "cant fork $!\n"; } } foreach my $href (@child_pids) { print "Waiting $href->{PID} | $href->{PROCESS}\n"; while (waitpid($href->{PID}, WNOHANG) == 0) { sleep 1; } print "Failed $href->{PROCESS}\n" unless($? == 0); print "Done $href->{PID} | $href->{PROCESS}\n"; } }