Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/local/roadm/bin/perl use strict; use warnings; use Net::SSH::Perl; print "Starting main program\n"; my @childs; my $user = "root"; my $handle; my $host = "zeus-b001"; $handle = Net::SSH::Perl->new($host); $handle->login($user, $user); my $cmd = "sleep 1"; my $pid = fork(); if ($pid) { # parent push(@childs, $pid); print "child pid is [$pid]\n"; } elsif ($pid == 0) { # child my $ret = sub1($handle, $cmd); print "ret is $ret\n"; exit 0; } else { die "couldnt fork: $!\n"; } foreach my $pid (@childs) { my $tmp = waitpid($pid, 0); print "done with pid $tmp\n"; } # The program hangs here my ($out,$err, $exit) = $handle->cmd("uname -a"); print "End of main program\n"; sub sub1 { my ($handle,$cmd) = @_; print "started child process\n"; my ($out,$err,$exit) = $handle->cmd($cmd); print "done with child process\n"; return 1; }

The program hangs in the parent process at the stmt my ($out,$err, $exit) = $handle->cmd("uname -a"); If I comment out the "my ($out,$err,$exit) = $handle->cmd($cmd);" in the subroutine sub1(), then parent process do not hang. Is there a solution to this issue or am I doing something wrong? Thanks.

Replies are listed 'Best First'.
Re: Net::SSH::Perl and fork hangs
by perlfan (Parson) on Jun 02, 2014 at 18:47 UTC
    You may want to not share $handle among children and parent.

      If I don't share the handle, I will have to initiate a new Net::SSH::Perl connection in child process and that will slow down the execution of my test case. I have a requirement to execute the same cmd on n number of hosts. I do have a workaround of using system(), instead of Net::SSH::perl. But using system() is slow. Thanks for your suggestion.

        Which runs slower? a) A new connection + system() or b) A Code that hangs and never completes?

        And, since this is a 'test'case', why is there an artificial constraint on the amount of time it takes? Tests typically need run to completion, how ever long that is. There are better things to focus your optimization efforts upon.

        Update: Looking through the old diaries I found the test logs from a gig I had at a bank in 1995. We were happy when the one test that exercised all of the databases, some 20+ Oracle DBs, completed in under 17 hours. Now we only ran it once a month as part of the regression testing, but....

        ----
        I Go Back to Sleep, Now.

        OGB