#!/bin/bash # auto-stop guard (T2b): stop when idle (no ssh session, load1 < 1.0) for 45 min; hard ceiling 24 h after boot echo 86400 > /etc/drums-guard.ceiling_s cat > /usr/local/sbin/drums-guard.sh <<'G' #!/bin/bash f=/run/drums-guard.idle; up=$(cut -d. -f1 /proc/uptime) [ -e /etc/drums-guard.ceiling_s ] && ceil=$(cat /etc/drums-guard.ceiling_s) || ceil=86400 if [ "$up" -gt "$ceil" ]; then logger drums-guard ceiling; shutdown -h now; fi l=$(cut -d' ' -f1 /proc/loadavg); s=$(who | wc -l); a=$(ss -tn state established '( sport = :22 )' | tail -n +2 | wc -l) if [ -e /run/drums-guard.hold ] || [ "$s" -gt 0 ] || [ "$a" -gt 0 ] || awk "BEGIN{exit !($l >= 1.0)}"; then rm -f $f; exit 0; fi [ -e $f ] || date +%s > $f if [ $(( $(date +%s) - $(cat $f) )) -gt 2700 ]; then logger drums-guard idle; shutdown -h now; fi G chmod +x /usr/local/sbin/drums-guard.sh echo '* * * * * root /usr/local/sbin/drums-guard.sh' > /etc/cron.d/drums-guard