Holland Wilson
Holland Wilson

Reputation: 701

Expect not waiting for spawned process

I'm trying to write an expect 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 autossh and other automatic tunneling solutions aren't relevant here because what I'm trying to do involves expect.) This is the complete script with troubleshooting messages.

#!/usr/bin/expect

set timeout 1

while { $timeout == 1 } {
  spawn -noecho ssh -f -N -T -R22224:localhost:22223 [email protected]
  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."

The ssh command works, and the expect man page says that wait 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 pkill many sshd instances on my remote machine). I've tried wait spawn, wait spawn_id, expect {ssh*} (which, according to the man page, should also make the script wait), but nothing will make the script pause.

Upvotes: 0

Views: 1645

Answers (1)

pynexj
pynexj

Reputation: 20688

ssh -f will fork a child process (running in background) and the parent process simply exits. Expect's wait command only waits for the spawned parent process so wait will return very soon.

Upvotes: 1

Related Questions