Skip to content

Instantly share code, notes, and snippets.

@auwsom
Last active March 14, 2020 04:22
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 auwsom/df90768bd9ef29bde3ea47e464f0d549 to your computer and use it in GitHub Desktop.
Save auwsom/df90768bd9ef29bde3ea47e464f0d549 to your computer and use it in GitHub Desktop.
# wget https://gist.github.com/auwsom/df90768bd9ef29bde3ea47e464f0d549/raw
# https://stackoverflow.com/questions/30556920/how-can-i-automatically-kill-idle-gce-instances-based-on-cpu-usage
#!/bin/bash
threshold=1
count=0
while true
do
load=$(awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 1000 / (t-t1); }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat))
load2=$(printf "%.0f\n" $load)
echo $load
echo $load2
if [[ $load2 -lt $threshold ]]
then
echo "Idling.."
((count+=1))
fi
echo "Idle minutes count = $count"
if (( count>10 ))
then
echo Shutting down
# wait a little bit more before actually pulling the plug
sleep 300
history -a
sudo poweroff
fi
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment