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

Here is the code that isn't working:
open(LS, qq{/usr/local/openssh/bin/ssh -p 22 foo\@localhost "if [ -d / +home/foo/bar/20110826 ] ; then echo 1 ; else echo 0 ; fi" |}); my $baz = <LS>;

The code hangs on me indefinitely. The strange thing is that, if I put it in a one-liner, it runs fine. Any ideas?

EDIT: I found the issue. The code had set

local $SIG{CHLD} = sub { 1 until waitpid(-1 , WNOHANG) == 1; };
to solve another issue, and this caused the process to sit in an infinite loop after termination.

Replies are listed 'Best First'.
Re: open on ssh command stuck
by onelesd (Pilgrim) on Aug 26, 2011 at 23:12 UTC
    Working for me. But, if <LS> is hanging it has to be because no \n is received. Add 2>&1 to the open command, maybe you are getting an error you aren't seeing due to not capturing STDERR.
    #!/usr/bin/perl open(LS, qq{ssh -p 22 onelesd\@localhost "if [ -d /home/foo/bar/201108 +26 ] ; then echo 1 ; else echo 0 ; fi" 2>&1 |}); my $baz = <LS>; print $baz ;
    MacBook-Pro:Downloads onelesd$ ./script.pl 0 MacBook-Pro:Downloads onelesd$
      That didn't seem to help. How would I be able to cause a \n to be received?