首页 > 解决方案 > Parallel::ForkManager 给出 - 子进程 STDIN 不是真正的系统错误

问题描述

我有一个脚本,如果服务器(@hosts)存储在数组中并在这些主机上执行某些命令,它会执行 ssh 编号。

...
use Net::OpenSSH;
use Parallel::ForkManager;
 
#these hosts are the IP addresses
my @hosts = ("host1", "host2", "host3", "host4", "host5"); 

my $ssh;
my $command = "perl -e "print \"Hello..\"";

my $pm = Parallel::ForkManager->new(5);
 
LOOP:
foreach my $n (@hosts) {
  my $pid = $pm->start and next LOOP;
 
  #doing ssh to the host and running the command
  #lets say here I am executing simple Perl command 
  $ssh = Connect($n, "user", "passwd");
  $ssh->capture($command);
 
  $pm->finish;
}
$pm->wait_all_children;
undef $ssh;

sub Connect {
    my ( $host, $user, $passwd ) = @_;
    my $ssh = Net::OpenSSH->new($host, user=>$user, password=>$passwd);
    $ssh->error and die "Couldn't establish SSH connection: " . $ssh->error;
    return $ssh;
}
...

当我执行这个脚本时,我遇到了错误,这对我来说就像噩梦一样。

child process STDIN is not a real system file handle at (eval 2) line 145

我不知道为什么会发生此错误。有人可以帮我解决这个问题。

我错过了什么$pm->finish还是需要添加$pm->run_on_finish(..)

标签: perlevalstdinfile-descriptoropenssh

解决方案


推荐阅读