Skip to content

Instantly share code, notes, and snippets.

@shal
Last active June 9, 2019 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shal/cce40c8c46f6eb209d20c081cefdf4e0 to your computer and use it in GitHub Desktop.
Save shal/cce40c8c46f6eb209d20c081cefdf4e0 to your computer and use it in GitHub Desktop.
Google Cloud Scheduler for VMs
#!/bin/bash -ex
# Author: Ali Shanaakh <hi@shal.dev>
check_vms() {
local PROJECT="gcloud-project"
local time=`date +%k%M`
local output=`gcloud compute --project "${PROJECT}" instances list --filter="labels.infinite: false" --format='table(name)'`
for instance in ${output[@]:4}; do
echo "Time now: $time"
if [[ $(eval "$time") -ge 1900 ]] && [[ $(eval "$time") -le 0600 ]]; then
echo "Starting virtual machinies with label infinite=false..."
gcloud compute --project "${PROJECT}" instances start $instance --zone "europe-west1-b"
else
echo "Stopping virtual machinies with label infinite=false..."
gcloud compute --project "${PROJECT}" instances stop $instance --zone "europe-west1-b"
fi
done
}
check_vms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment