I'm trying to create a script that runs a few commands that take a long time to execute and require a constant connection, but
autossh user@server
wait $! #or wait ${!}
commandA
commandB
doesn't work (I'm assuming because autossh
drops into the background). The GUI pops up asking for a password and while that is up the next few commands are run.
How do I wait for an established connection before running the next commands?
Update:
autossh -f => "…causes autossh to drop to the background before running ssh…"
so with the -f wait does nothing, without it, you wait on the ssh which will never end…
Best Answer
If I understand you correctly your problem is that you want to wait until authentication succeeds before continuing with the rest of your script.
I would solve this with public key authentication and ssh-agent.
Once this is configured and ssh-agent is running, ssh won't ask for a password and your script will authenticate correctly each time. (It's not quite the same as ensuring the connection is working but it may suffice for your purposes)
Two other workarounds come to mind: