Google Cloud Platform (GCP) instance idle shutdown
source link: https://gist.github.com/justinshenk/312b5e0ab7acc3b116f7bf3b6d888fa4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Google Cloud Platform (GCP) instance idle shutdown · GitHub
Instantly share code, notes, and snippets.
Justin, could you elaborate on how to implement this script? I am new to bin/bash/.sh stuff.
I have created a win10 vm in GCP and I want it to auto shut down after 60 min of inactivity.
Thanks,
Amit
If you are still looking for the answer by anychance here it is below,
Go to GCP shell, create the above file there and run below
gcloud compute instances add-metadata nested-vm-image1 --zone=<> --metadata-from-file startup-script=idle-shutdown.sh
If you are still looking for the answer by anychance here it is below,
Go to GCP shell, create the above file there and run below
gcloud compute instances add-metadata nested-vm-image1 --zone=<> --metadata-from-file startup-script=idle-shutdown.sh
@viveksamaga I think @morphcatalyst was asking about a way to do this on a win10 VM. I'm in a similar situation. I don't think Windows works with bash, so it won't be able to execute that startup script?
Shouldn't count be reset to 0 if res is False?
jarrodonlo commented on Jan 6
I'm curious about @bcli4d comment...
Shouldn't count be reset to 0 if res is False?
...isn't he right, shouldn't count=0
?
jarrodonlo commented on Jan 6
I'm curious (but not trying to imply your method is "wrong") why are you not running this as a cron
job?
jarrodonlo commented on Jan 6
I really appreciate the script. Idle shutdown is a feature that I want in a VM. It gives me peace of mind that my bill won't creep up for unused dev box hours. I see that GCP's Datalab has a beta VM Auto Shutdown feature, so maybe we'll get it backed in with other GCP VMs in the future. I believe that Azure already has such a feature baked in ...but who has time to get familiar with another cloud and tooing?
Good point, no reason not to do a cron job. Feel free to share your working script here as well
jarrodonlo commented on Jan 6
I'm not as familiar with GCP VM's metadata
and startup-scripts
, but looking at it further I definitely see advantages (opposed to cron) to doing it the way you are.
- No need to keep track of state between cron invocations.
- Seems more modular for other VMs and groups.
but I'm still curious..
Shouldn't count be reset to 0 if res is False?
ravwojdyla commented on Jan 29 •
Here's a version that suspends (check the limitation of suspend here) the VM if the 15' load average is consecutively less than the threshold (default: 1 core) for an hour. One way to run it is: nohup bash auto_shutdown.sh &>/dev/null &
(in your startup script or manually).
#!/bin/bash
threshold=${1:-1}
intervals=${2:-60}
sleep_time=${3:-60}
function require() {
if ! which $1 >/dev/null; then
echo "This script requires $1, aborting ..." >&2
exit 1
fi
}
require gcloud
require python3
require curl
if ! curl -s -i metadata.google.internal | grep "Metadata-Flavor: Google" >/dev/null; then
echo "This script only works on GCE VMs, aborting ..." >&2
exit 1
fi
COMPUTE_METADATA_URL="http://metadata.google.internal/computeMetadata/v1"
VM_PROJECT=$(curl -s "${COMPUTE_METADATA_URL}/project/project-id" -H "Metadata-Flavor: Google" || true)
VM_NAME=$(curl -s "${COMPUTE_METADATA_URL}/instance/hostname" -H "Metadata-Flavor: Google" | cut -d '.' -f 1)
VM_ZONE=$(curl -s "${COMPUTE_METADATA_URL}/instance/zone" -H "Metadata-Flavor: Google" | sed 's/.*zones\///')
count=0
while true; do
load=$(uptime | sed -e 's/.*load average: //g' | awk '{ print $3 }')
if python3 -c "exit(0) if $load >= $threshold else exit(1)"; then
echo "Resetting count ..." >&2
count=0
else
((count+=1))
echo "Idle #${count} at $load ..." >&2
fi
if ((count>intervals)); then
if who | grep -v tmux 1>&2; then
echo "Someone is logged in, won't shut down, resetting count ..." >&2
else
echo "Suspending ${VM_NAME} ..." >&2
gcloud beta compute instances suspend ${VM_NAME} --project ${VM_PROJECT} --zone ${VM_ZONE}
fi
count=0
fi
sleep $sleep_time
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK