#!/bin/bash # Auto-stop guard installed at launch (charter rule): hard ceiling + idle rule. Shutdown behavior of the instance is "stop". # HARD_H: hours after boot at which the host powers off no matter what. IDLE_MIN: power off after this many minutes with # load1 < 2, no sshd session and no file /run/t1-busy (the bench driver touches it while it runs). HARD_H=${HARD_H:-14}; IDLE_MIN=${IDLE_MIN:-40} cat > /usr/local/sbin/t1-guard </dev/null)" ] && busy=1 pgrep -f 'sshd(-session)?: [a-z]+@' >/dev/null && busy=1 awk '{exit !(\$1>=2)}' /proc/loadavg && busy=1 if [ \$busy = 1 ]; then date +%s > /run/t1-last-busy; exit 0; fi last=\$(cat /run/t1-last-busy 2>/dev/null || echo \$(date +%s)); [ -f /run/t1-last-busy ] || echo \$last > /run/t1-last-busy [ \$(( \$(date +%s) - last )) -ge $((IDLE_MIN*60)) ] && { logger t1-guard idle; systemctl poweroff; } exit 0 G chmod 755 /usr/local/sbin/t1-guard cat > /etc/systemd/system/t1-guard.service < /etc/systemd/system/t1-guard.timer <