在CentOS 6和Ubuntu 20.04上捕获不同子进程的退出状态(在Perl中)



我正在将一个perl备份脚本从CentOS 6改编为Ubuntu 20.04。

子程序ExecCmd((为系统调用rsync启动一个子进程。

在CentOS6上,它用rsync输出填充变量$ExecCmdOut,并返回退出状态$pipestatus。

在Ubuntu上$pipestatus包含rsync输出的最后一行。在日志中,我看到

Error (573): rsync failed with status total size is 2,728,691,525  speedup is 509.15.

这是函数。你明白为什么吗?

sub ExecCmd( @$ ) {
(my $cmdRef, my $forcelog) = @_;
my @cmd = @$cmdRef;
my $pipestatus='';
die "Fork failed: $!n" unless defined( my $pid=open(RCHILD, "-|"));
if( $pid ) { 
$ExecCmdOut='';
while(<RCHILD>) {
chomp( $_ );
next if $_ eq '';
s/e[[0-9;]+[A-Za-z]//g; # remove ANSI escape sequences
$ExecCmdOut.="$_n";
$pipestatus=$_;
}
close( RCHILD );
} else {
exec( "@cmd 2>&1; echo ${PIPESTATUS}" ) or die "exec failed: $!n";
}
$ExecCmdOutout =~ s/$pipestatusn$//;
$pipestatus =  $? if not $pipestatus;
return $pipestatus;
}

您在此处用当前行填充$pipestatus:

$pipestatus=$_;

最后,你把它设置为$?仅当$pipestatus为false时。如果它包含最后一行,则它不是false,因此不会更改为$?。

最新更新