in reply to Net::OpenSSH and fork()

What is happening is that the ssh master process is killed in the DESTROY method that gets called in every forked process when they exit. The next release of Net::OpenSSH will have code to detect that condition and only kill the master from the same process where it was launched.

Anyway, when forking in perl it is usually a good idea to exit from the child processes using POSIX::_exit($code) that will NOT execute any cleanup code as destructors or END blocks.

Besides that, that is not the right way to use Net::OpenSSH that has built in support for running remote operations asynchronously (search in the docs for async or/and as sflitman has already pointed out, use the spawn method).

Finally, there is also Net::OpenSSH::Parallel:

use Net::OpenSSH::Parallel; my $pssh = Net::OpenSSH::Parallel->new; for my $server (@targets) { $pssh->add_host($server); } $pssh->push('*', command => @stuff); $pssh->push('*', command => @more_stuff); $pssh->run; # ... $pssh->push('*', command => @even_more_stuff); $pssh->run;

Replies are listed 'Best First'.
Re^2: Net::OpenSSH and fork()
by scotchie (Novice) on Aug 03, 2010 at 18:55 UTC

    Thanks all for the help and suggestions!

    Net::OpenSSH::Parallel looked promising. But at the time I started the project, the CPAN documentation had a note that it was still an alpha version. So I did not install it.

    My example used the fork() function, but I actually use forks.pm. I like the Perl threads feature, but my binary is not complied with support for it. Among other things, I need to run other tasks in parallel besides just SSH commands, and I also need to do some IPC between the processes. The Perl threads feature is a lot easier to use for IPC than pipes and other pure-UNIX solutions.

    I just downloaded version 0.48 of the module and modified the DESTROY method, from

    if ($pid) {

    to

    if ($pid && $perl_pid == $$) {

    This solves the problem.

    Best Regards,
    Scott
      the CPAN documentation had a note that it was still an alpha version

      Yes, it still says so, I am probably being too conservative there, I should move it to beta!

        I noticed that Net::OpenSSH version 0.49 includes the fix. Thanks!