Home > Enterprise >  How to Wait the command to finish - gcloud config configurations activate
How to Wait the command to finish - gcloud config configurations activate

Time:10-27

I have a bash script which activate multiples configurations with following gcloud command:

gcloud config configurations activate MYENV

In the most case, this command works instantly, sometimes it takes 10 secondes or more than 60 secondes.

I am using sleep(10) ... but it is not the best way. Anyone, have an idea on how to wait that gcloud command finish?

Even if I can use return=$? , I will not know if this command waits for the command to be finished.

CodePudding user response:

You can use the wait command to use for the last command identified by its PID:

#!/bin/usr/env bash

# launch your command in the background
gcloud config configurations activate MYENV &

# wait for the command to finish
wait $!

echo 'Configuration $MYENV activited'

CodePudding user response:

I'm breaking a personal practice by answering a question by proposing an alternative approach but...

gcloud config configurations adjusts gcloud's global state by swapping in a set of configuration variables. These variables are all readily represented directly in gcloud commands.

Particularly when (writing) scripts, I feel a better practice is to use flags and variables on commands explicitly.

Rather than:

for CONFIG in "foo" "bar" "baz"
do
  gcloud config configurations activate ${CONFIG}
  gcloud compute instances delete instance-01 \
  --quiet
done

Where parameters (flags) of gcloud compute instances delete insurance-01 are implicit.

Versus:

for PROJECT in "foo" "bar" "baz"
do
  for ZONE in "us-west1-c" "us-central1-a"
  do
    gcloud compute instances delete instance-01 \
    --zone=${ZONE} \
    --project=${PROJECT} \
    --quiet
  done
done

Where you:

  • avoid any global update "pauses"
  • avoid breaking a gcloud session running in a different process
  • have a more explicit and understandable set of commands
  • can pump the script output to a log file to see exactly what ran

Personally, I discourage use of gcloud config for anything other than the unavoidable i.e. gcloud config ... account and that may be all.

  • Related