Last active
April 17, 2018 07:20
-
-
Save tetsu-koba/d1dbefac761aea5bc5c402c1465db04b to your computer and use it in GitHub Desktop.
GCE auto shutdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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