1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
package CMD_processor;
use strict;
use warnings;
use Carp;
use vars qw (@ISA @EXPORT); ## set in script using this module for verbose output.
@ISA = qw(Exporter);
@EXPORT = qw(process_cmd process_parallel_cmds);
####
sub process_cmd {
my ($cmd) = @_;
print STDERR "CMD: $cmd\n";
my $ret = system($cmd);
if ($ret) {
die "Error, cmd: $cmd died with ret ($ret)";
}
return; # all good.
}
####
sub process_parallel_cmds {
my (@cmds) = @_;
foreach my $cmd (@cmds) {
my $pid = fork();
unless ($pid) {
# child
&process_cmd($cmd);
exit(0);
}
}
my $process_failed = 0;
while (my $pid = wait()) {
if ($pid < 0) {
last;
}
else {
if ($?) {
print STDERR "-process $pid exited ($?) indicating error.\n";
$process_failed = 1;
}
}
}
if ($process_failed) {
die "Error, at least one command failed. See above errors for more details. ";
}
return; # all good.
}
1; #EOM
|