in reply to Net::OpenSSH how to stop(zap) capturing after time

Use the remote shell to kill the command after 10 seconds:
$out = $ssh->capture({stderr_to_stdout => 1}, "$cmd & sleep 10; kill $!");

Replies are listed 'Best First'.
Re^2: Net::OpenSSH how to stop(zap) capturing after time
by MiklerGM (Novice) on Mar 22, 2011 at 10:34 UTC

    Wow, awesome, but... If i set timeout to 1 and command to `cat /dev/urandom` it takes only 13 seconds ( "$cmd & sleep $timeout; kill \$!" )
    And if command is `echo Hello, World` script returns Exit Code 1, but i think i can fix it :)

      You could probably write a more reliable wrapper in Perl, and install it on your remote servers, or use a one-liner.

      Another option is to write your own time-limited capture method. Use pipe_out to launch the remote command and read from the returned pipe until the timeout expires. Closing the pipe will deliver a SIGPIPE to the remote process killing it.

        Thanks, it works
        my ($out, $pid) = $ssh->pipe_out("$cmd"); my $sel = new IO::Select($out); my $str = ""; while (1) { @ready = $sel->can_read(1); if(@ready > 0) { if(my $new = <$out>) { $str .= $new; } else { last; } } if ($ConnectionDeadline < time()) { kill 15, $pid; $str .= "\nConnection timeout\n"; last; } }