Skip to content

Instantly share code, notes, and snippets.

@Jacse
Jacse / vm_autoshutdown_on_idle.sh
Created February 9, 2023 17:06
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.
#!/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)