Script Won't Exit After Call to System

Discussion in 'Ruby' started by David Sokoloff, Mar 8, 2009.

  1. 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.
    --
    Posted via http://www.ruby-forum.com/.
    David Sokoloff, Mar 8, 2009
    #1
    1. Advertising

  2. 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.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Mar 8, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,895
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Kevin
    Replies:
    0
    Views:
    348
    Kevin
    Apr 30, 2004
  3. Replies:
    2
    Views:
    453
    Jeff Epler
    May 31, 2005
  4. Debo
    Replies:
    3
    Views:
    170
  5. kaleem
    Replies:
    8
    Views:
    813
    Tad McClellan
    Dec 14, 2006
Loading...

Share This Page