I'm using the following line in my scripts:
ssh -f -N -M -S <control socket> <host>
This means the initial connection just stays in the background and I can use it for subsequent calls to
ssh -S <control socket> <host> <command>
However, if I have multiple scripts with commands which are supposed to use the same control socket and put the "background" call to
ssh into all of them, I will get the following message at some point:
ControlSocket <control socket> already exists, disabling multiplexing
This has no influence over the rest of the script because obviously the socket exists and can be used by the subsequent
ssh commands. However, even though the "background" session couldn't open the socket, it doesn't quit and stays active - just without multiplexing.
[ -S or
ssh -O check to check the existence of the socket would still leave the possibility of race conditions.
How can I do "open control socket if it doesn't exist yet, and exit if it does"?