Skip to content

Instantly share code, notes, and snippets.

@odiak
Created June 24, 2019 12:55
Show Gist options
  • Save odiak/b1e809458bdb885ab9bbd3d58b6616d5 to your computer and use it in GitHub Desktop.
Save odiak/b1e809458bdb885ab9bbd3d58b6616d5 to your computer and use it in GitHub Desktop.
#!/bin/bash
TIMESTAMP_FILE="/tmp/auto-stop-timestamp"
if [ $(who | wc -l) -gt 0 ]; then
rm -f $TIMESTAMP_FILE
else
now=$(date "+%s")
if [ -f "$TIMESTAMP_FILE" ]; then
echo $now > $TIMESTAMP_FILE
else
t=$(cat $TIMESTAMP_FILE)
if [ $(($now - $t)) -gt 3600 ]; then
gcloud compute instances stop my-instance-name
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment