Skip to content

Instantly share code, notes, and snippets.

@Matrix7867
Created February 13, 2020 09:18
Show Gist options
  • Save Matrix7867/3fd36a79ea0a4289aec2fc88c36259ca to your computer and use it in GitHub Desktop.
Save Matrix7867/3fd36a79ea0a4289aec2fc88c36259ca to your computer and use it in GitHub Desktop.
terminate-gcp-instance-based-on-file-exists-or-not
echo "Termination of Instance process will start now"
curl -s http://metadata.google.internal/computeMetadata/v1/instance/name -H "Metadata-Flavor: Google" > /home/ubuntu/a
touch /home/ubuntu/_SUCCESS
filey="/home/ubuntu/_SUCCESS"
if [ -f "$filey" ]; then
cat /home/ubuntu/a | xargs -I {} gcloud compute instances delete {} --zone=us-central1-a --delete-disks=all --quiet
echo "Instance will get terminated"
else
cat /home/ubuntu/a | xargs -I {} gcloud compute instances stop {} --zone=us-central1-a --quiet
echo "Instance will gonna stop"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment