# Script Won't Exit After Call to System

**URL:** <https://www.ruby-forum.com/t/script-wont-exit-after-call-to-system/162332>\
**Category:** Ruby\
**Created:** [March 8, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/script-wont-exit-after-call-to-system/162332 "2009-03-08T19:44:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 8, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/script-wont-exit-after-call-to-system/162332/1 "2009-03-08T19:44:58Z")

</div>

I have a small script (see [http://pastie.org/411010](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.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 8, 2009, 8:42pm UTC](https://www.ruby-forum.com/t/script-wont-exit-after-call-to-system/162332/2 "2009-03-08T20:42:45Z")

</div>

David Sokoloff wrote:

> I have a small script (see [http://pastie.org/411010](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:

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](http://host.example.com)

HTH,

Brian.
