Skip to content

Instantly share code, notes, and snippets.

@pydevops
Last active September 11, 2020 19:23
Show Gist options
  • Save pydevops/9000e2aed5346ede351b9df36df31b42 to your computer and use it in GitHub Desktop.
Save pydevops/9000e2aed5346ede351b9df36df31b42 to your computer and use it in GitHub Desktop.
gcp start stop tagged instance script for cost saving
#!/bin/bash
set -euo pipefail
start_stop() {
while read name zone ; do
echo "$name with $zone"
gcloud compute instances $CMD $name --zone $zone &
done < <(gcloud compute instances list --filter='tags.items~^stoppable$' --format='value(NAME,ZONE)')
wait
gcloud compute instances list --filter=running
echo "$CMD are done!"
}
CMD=$1
gcloud compute instances list --filter=running
read -p "Do you want to proceed?" yn
case $yn in
[Yy]* ) start_stop;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment