title: Expect not waiting for spawned process
body: <p>I'm trying to write an <code>expect</code> script to start an SSH tunnel, wait for the tunnel process to close (throwing an error) and then rerun the tunnel command. (It's worth noting that <code>autossh</code> and other automatic tunneling solutions aren't relevant here because what I'm trying to do involves <code>expect</code>.) This is the complete script with troubleshooting messages.</p>
  
  <pre><code>#!/usr/bin/expect
  
  set timeout 1
  
  while { $timeout == 1 } {
    spawn -noecho ssh -f -N -T -R22224:localhost:22223 username@xxx.xxx.xxx.xxx
    send_user "Looking for host"
    set timeout 0
    wait
    set timeout 1
    send_user "We've encountered an error. Re-running."
  }
  
  send_user "Out of the loop."
  </code></pre>
  
  <p>The <code>ssh</code> command works, and the <a href="http://linux.die.net/man/1/expect" rel="nofollow"><code>expect</code> man page</a> says that <code>wait</code> should hold the script until the current spawned process dies, but it doesn't and it ends up rerunning the loop a whole bunch of times (forcing me to <code>pkill</code> many <code>sshd</code> instances on my remote machine). I've tried <code>wait spawn</code>, <code>wait spawn_id</code>, <code>expect {ssh*}</code> (which, according to the man page, should also make the script wait), but nothing will make the script pause.</p>