It seems that you are using spawn in order to create detached processes on the remote host, but it doesn't work that way.

spawn forks a new local ssh process that continues running on the background until the remote process exits and you have to take care of reaping those ssh processes with waitpid, otherwise zombies will pile up and at some point the OS will refuse to fork new processes... and that is probably the reason for your script failing.

The right way to do that is to run the remote command with nohup:

$ssh->system("nohup ./script.pl &");
Though, as it seems that the remote command is actually a Perl script, you can also convert it into a daemon letting it take the responsibility of going into the background. There are several CPAN modules that allow to do that (i.e. Proc::Daemon).

Besides that, there are other places where you can improve your Net::OpenSSH usage:

# Read File on remote machine ( $DATA, $ERR ) = $SSH->capture2( "cat <file on remote machine" ); # Write/Append $DATA to loca file <write data to loca file>
...can just be written as...
$ssh->system({stdout_file => ['>>', $local_file]}, cat => $remote_file).
And in...
my $PROC = $SSH->capture( "ps aux | grep <process_name> | grep -v grep + | wc -l | sed 's/ *//'" ); if( $PROC == 0 ) {...
you should check that the command did not fail due to some SSH error. Otherwise, you could end running several instances of ./script.pl.

In reply to Re^3: Net::OpenSSH killing script by salva
in thread Net::OpenSSH killing script by jaiieq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.