Skip to content

Instantly share code, notes, and snippets.

@ryancdotorg
Created February 28, 2017 14:32
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 ryancdotorg/e008735609c6a1cbfb870100b4e99f2d to your computer and use it in GitHub Desktop.
Save ryancdotorg/e008735609c6a1cbfb870100b4e99f2d to your computer and use it in GitHub Desktop.
shut down a system when it becomes idle - for aws spot instances
#!/bin/bash
IDLES=0
while sleep 30
do
PCT=`top -bn 2 -d 0.1 | grep '^%Cpu' | tail -n 1 | tr :. ' ' | awk '{print$2}'`
if [ $PCT -lt 10 ]
then
IDLES=$[$IDLES+1]
#echo $IDLES $PCT
else
IDLES=0
fi
if [ $IDLES -ge 20 ]
then
sudo shutdown -h now
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment