mirror of https://github.com/digint/btrbk
btrbk: run_cmd(): catch all possible errors when executing system command
parent
e402435dc8
commit
5f867c2347
31
btrbk
31
btrbk
|
@ -691,22 +691,39 @@ sub run_cmd(@)
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
DEBUG "### $cmd_print";
|
DEBUG "### $cmd_print";
|
||||||
|
TRACE "Executing command: $cmd";
|
||||||
|
|
||||||
|
# disable warnings in this scope (e.g. "permission denied", "no such file"), these cases are handled below.
|
||||||
|
# NOTE: for some reason this is only needed if we use "use warnings FATAL => qw( all )"
|
||||||
|
no warnings qw(exec);
|
||||||
|
|
||||||
# execute command and parse output
|
# execute command and parse output
|
||||||
TRACE "Executing command: $cmd";
|
my $ret = `$cmd`;
|
||||||
my $ret = "";
|
if(defined($ret)) {
|
||||||
$ret = `$cmd`;
|
|
||||||
chomp($ret);
|
chomp($ret);
|
||||||
TRACE "Command output:\n$ret";
|
TRACE "Command output:\n$ret";
|
||||||
if($?) {
|
}
|
||||||
my $exitcode= $? >> 8;
|
|
||||||
|
if($? == -1) {
|
||||||
|
ERROR "Command execution failed ($!): `$cmd_print`";
|
||||||
|
return undef;
|
||||||
|
}
|
||||||
|
elsif ($? & 127) {
|
||||||
my $signal = $? & 127;
|
my $signal = $? & 127;
|
||||||
DEBUG "Command execution failed (exitcode=$exitcode" . ($signal ? ", signal=$signal" : "") . "): \"$cmd\"";
|
ERROR "Command execution failed (child died with signal $signal): `$cmd_print`";
|
||||||
|
return undef;
|
||||||
|
}
|
||||||
|
elsif($?) {
|
||||||
|
my $exitcode= $? >> 8;
|
||||||
|
DEBUG "Command execution failed (exitcode=$exitcode): `$cmd_print`";
|
||||||
|
|
||||||
if($catch_stderr) {
|
if($catch_stderr) {
|
||||||
$_ = $ret;
|
$_ = $ret;
|
||||||
&{$filter_stderr} ($cmd) if($filter_stderr);
|
&{$filter_stderr} ($cmd) if($filter_stderr);
|
||||||
ERROR "[$cmd_print] $_" if($_);
|
if($_) {
|
||||||
|
# no filter, or uncaught by filter
|
||||||
|
ERROR "Command execution failed (exitcode=$exitcode): `$cmd_print`: $_";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue