0

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?

anarchy
  • 3,709
  • 2
  • 16
  • 48
  • its not a gcloud specific solution... the question ur showing me only works if theres a variable. mine has no variable – anarchy Jan 15 '21 at 07:07
  • until passwd? how am i supposed to use that with an ssh command? it would work the same if i had done ssh -i .... without the gcloud. – anarchy Jan 15 '21 at 07:12
  • i mean my question has no variable, how am i supposed to use until or while? – anarchy Jan 15 '21 at 07:13
  • `until gcloud compute ssh ...; do sleep .1; done`? I still have no idea what you mean by value. – oguz ismail Jan 15 '21 at 07:17

0 Answers0