Script Won't Exit After Call to System

I have a small script (see http://pastie.org/411010) that will check to
see if my ssh tunnel has died, and if so, will create a new one. It
works, except that if it does need to re-open a tunnel (through Kernel’s
‘system’ call), the script never exits. Maybe it isn’t a big deal (say
so if you think so), but I’m not sure why the script doesn’t exit. It
does re-open the tunnel successfully. Furthermore, if you execute from
the command line the same ssh command I’m executing through Ruby’s
‘system’ call, the tunnel does successfully go to the background.

Thanks for any insight.

David Sokoloff wrote:

I have a small script (see http://pastie.org/411010) that will check to
see if my ssh tunnel has died, and if so, will create a new one. It
works, except that if it does need to re-open a tunnel (through Kernel’s
‘system’ call), the script never exits. Maybe it isn’t a big deal (say
so if you think so), but I’m not sure why the script doesn’t exit. It
does re-open the tunnel successfully. Furthermore, if you execute from
the command line the same ssh command I’m executing through Ruby’s
‘system’ call, the tunnel does successfully go to the background.

Thanks for any insight.

Rather than mess around with how the ssh command-line client may or may
not background itself, I suggest two alternatives:

  1. Use Net::SSH as the client and get it to do the forwarding for you.
    This is a pure Ruby solution and hence no issues with forked processes.
    From the docs:

    ssh.forward.local(17055, “mail.example.com”, 143)

  2. If you’re going to use the openssh client, run it in the foreground
    and hold onto it from the Ruby client. Then you’ll know immediately when
    it has terminated, zombie-free, and can restart it when necessary;
    scanning the output of ‘ps’ is no longer necessary.

You might do this using IO.popen or the pty library, but even if just
using system() this can be done in a Ruby thread.

Aside: I don’t know if you realise, but you can open multiple forwarding
ports in the same ssh command line:

ssh -Lxx:xx:xx -Lyy:yy:yy -Lzz:zz:zz host.example.com

HTH,

Brian.