Skip to content

Instantly share code, notes, and snippets.

@tetsu-koba
Last active April 17, 2018 07:20
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 tetsu-koba/d1dbefac761aea5bc5c402c1465db04b to your computer and use it in GitHub Desktop.
Save tetsu-koba/d1dbefac761aea5bc5c402c1465db04b to your computer and use it in GitHub Desktop.
GCE auto shutdown
#!/bin/sh
do_shutdown () {
wall "Auto shutdown!"
sync
# Replace next line to your shut down command
gcloud compute instances stop instance-N --zone\=us-central1-a && shutdown -h now
}
warning () {
wall "Auto shutdown will start after $1 min.
To cancel,
sudo killall autoshutdown.sh"
}
if [ $# -ge 1 ]; then
tx=$1
if [ $tx -le 60 ]; then
tx=60
fi
else
case `nproc` in
"1") tx=$((60*8));;
"2") tx=$((60*6));;
"3") tx=$((60*5));;
"4") tx=$((60*4));;
*) tx=$((60*3));;
esac
fi
(
sleep $(($tx - 60))m
tx=$(($tx - 60))
warning 60
sleep 45m
warning 15
sleep 10m
warning 5
sleep 5m
do_shutdown
sleep 5m
do_shutdown
)&
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment