Files
wehub-resource-sync a789495a98
FreeBSD Smoke / FreeBSD Smoke (x86_64) (push) Has been cancelled
CI / Quality Guardrails (push) Has been cancelled
CI / Build & Test (macos-latest) (push) Has been cancelled
CI / Build & Test (ubuntu-latest) (push) Has been cancelled
CI / Build & Test (windows-latest) (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / PowerShell Syntax (push) Has been cancelled
CI / Windows Cross-Target Check (Linux) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:34 +08:00

26 lines
795 B
Bash

#!/bin/bash
# Power off after 30 min with no clients AND no active agent work.
STATE_FILE=/var/tmp/idle-since
CONNS=$(ss -Htn state established "( sport = :7643 or sport = :22 )" | wc -l)
# jcode server doing outbound work (e.g. streaming from Bedrock)?
JPID=$(pgrep -f "jcode.*serve" | head -1)
BUSY=0
if [ -n "$JPID" ]; then
OUT443=$(ss -Htnp state established "( dport = :443 )" 2>/dev/null | grep -c "pid=$JPID") || true
[ "$OUT443" -gt 0 ] && BUSY=1
fi
if [ "$CONNS" -gt 0 ] || [ "$BUSY" -gt 0 ]; then
rm -f "$STATE_FILE"
exit 0
fi
NOW=$(date +%s)
if [ ! -f "$STATE_FILE" ]; then
echo "$NOW" > "$STATE_FILE"
exit 0
fi
IDLE_SECS=$((NOW - $(cat "$STATE_FILE")))
if [ "$IDLE_SECS" -ge 1800 ]; then
logger "idle-autostop: idle ${IDLE_SECS}s, powering off"
systemctl poweroff
fi