无法在perl中处理Psexec命令的异常处理

时间:2017-06-27 09:03:23

标签: perl cmd psexec

如果for命令失败,我试图退出psexec循环,或者更重要的是,如果它需要更多时间,我在此程序中使用Time::Out模块,但是{{ 1}}命令没有退出,我使用它错了吗?有没有其他方法来处理这种情况。

psexec

1 个答案:

答案 0 :(得分:1)

为什么SIGALRM无法传递到某个进程,并且您发现其中一个进程,有多种原因。解决方法是poor man's alarm - 在后台运行一个单独的进程来监视长时间运行的进程并在一段时间过后将其终止。在Windows上以及需要捕获输出的命令,它看起来像这样:

# $pid is the id of the process to monitor
my $pid = open my $proc_fh, '-|', "psexec \\\\$host1 cmd /c "dir /s d:\\dd\";

my $pma = "sleep 1,kill(0,$pid)||exit for 1..$nb_secs;"
        . "kill -9,$pid;system 'taskkill /f /pid $pid >nul'";
my $pma_pid = system 1, $^X, "-e", $pma;

# read input from the external process.
my $returnFilenames;
while (<$proc_fh>) {
    $returnFilenames .= $_;
}
close $proc_fh;

这个可怜人的闹钟是一个有两个部分的小程序。第一部分计数最多$nb_secs秒,如果受监视的进程不再运行(如果kill 0,$pid返回false值),则终止。如果到达程序的第二部分,则终止受监视的进程。在这种情况下,我们尝试两种不同的方式(kill -9 ...system 'taskkill /f ...')来终止该过程。