Script Won't Exit After Call to System

D

David Sokoloff

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.
 
B

Brian Candler

David said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top