I have a script called test.sh
that looks like this,
#!/bin/bash
gcloud compute instances start node-1
gcloud compute instances attach-disk node-1 --disk=disk-1
gcloud compute ssh user@node-1 --command "some command"
It turns on a vm in google cloud, attaches a disk, and runs a command.
However, sometimes, I get this error,
ssh: connect to host xx.xx.xx.xx port 22: Connection refused
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].
It's because the server hasn't been fully initialized. If I wait another minute it works fine.
I know I can add a sleep to it but that seems inefficient.
How can I add an exception so that if an error is throwing, I can repeat the command until it is successfully executed?
Is there a google cloud command to check if a server has been fully initialised?