Created
February 9, 2023 17:06
-
-
Save Jacse/425dea2f4ad5be7c66b37b51d978dffd to your computer and use it in GitHub Desktop.
A script that automatically shuts down a VM if it has been idle for more than 10 minutes in a row. Based on https://gist.github.com/justinshenk/312b5e0ab7acc3b116f7bf3b6d888fa4 but also includes a check to ensure it doesn't shutdown in the first 10 minutes of lifetime.
This file contains hidden or 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/bash | |
threshold=0.05 | |
count=0 | |
ten_min_check="up [0-9]{1} min" | |
while true | |
do | |
load=$(uptime | sed -e 's/.*load average: //g' | awk '{ print $3 }') | |
res=$(echo $load'<'$threshold | bc -l) | |
if [[ uptime =~ $ten_min_check ]] | |
then | |
echo "First 10 minutes of boot" | |
elif (( $res )) | |
then | |
echo "Idling.." | |
((count+=1)) | |
else | |
echo "Working.." | |
count=0 | |
fi | |
echo "Idle minutes count = $count" | |
if (( count>10 )) | |
then | |
echo Shutting down | |
# wait a little bit more before actually pulling the plug | |
sleep 120 | |
sudo poweroff | |
fi | |
sleep 60 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment