From a1e60a9840f389a9f6c370f42ab19fca552f49bd Mon Sep 17 00:00:00 2001 From: kim Date: Sun, 18 Oct 2026 21:28:32 +0900 Subject: [PATCH] =?UTF-8?q?=EC=98=A8=EC=8B=A4=20=ED=8C=8C=EC=9D=B4:=20?= =?UTF-8?q?=EB=84=A4=ED=8A=B8=EC=9B=8C=ED=81=AC=20=EA=B0=90=EC=8B=9C(?= =?UTF-8?q?=EA=B3=B5=EC=9C=A0=EA=B8=B0=20=ED=95=91=20=EC=8B=A4=ED=8C=A8=20?= =?UTF-8?q?=EC=8B=9C=20wlan0=20=EC=9E=AC=EC=97=B0=EA=B2=B0=E2=86=92?= =?UTF-8?q?=EB=93=9C=EB=9D=BC=EC=9D=B4=EB=B2=84=20=EC=9E=AC=EC=A0=81?= =?UTF-8?q?=EC=9E=AC=E2=86=92=EC=9E=AC=EB=B6=80=ED=8C=85)=20=EC=8A=A4?= =?UTF-8?q?=ED=81=AC=EB=A6=BD=ED=8A=B8=C2=B7=ED=83=80=EC=9D=B4=EB=A8=B8=20?= =?UTF-8?q?=EB=B3=B4=EA=B4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- devices/pi2-greenhouse/netwatch.service | 5 +++++ devices/pi2-greenhouse/netwatch.sh | 24 ++++++++++++++++++++++++ devices/pi2-greenhouse/netwatch.timer | 8 ++++++++ 3 files changed, 37 insertions(+) create mode 100644 devices/pi2-greenhouse/netwatch.service create mode 100644 devices/pi2-greenhouse/netwatch.sh create mode 100644 devices/pi2-greenhouse/netwatch.timer diff --git a/devices/pi2-greenhouse/netwatch.service b/devices/pi2-greenhouse/netwatch.service new file mode 100644 index 0000000..993a14b --- /dev/null +++ b/devices/pi2-greenhouse/netwatch.service @@ -0,0 +1,5 @@ +[Unit] +Description=Network watchdog (ping gateway, recover Wi-Fi) +[Service] +Type=oneshot +ExecStart=/usr/local/sbin/netwatch.sh diff --git a/devices/pi2-greenhouse/netwatch.sh b/devices/pi2-greenhouse/netwatch.sh new file mode 100644 index 0000000..fe7dfff --- /dev/null +++ b/devices/pi2-greenhouse/netwatch.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# 공유기(.1)에 핑이 연속으로 안 되면 단계적으로 복구한다. +# 3회(약 3분): wlan0 재연결 / 6회: Wi-Fi 드라이버 재적재 / 10회(약 10분): 재부팅(부팅 후 15분 이내면 생략 — 재부팅 반복 방지) +GW=192.168.0.1 +F=/run/netwatch.fails +n=$(cat "$F" 2>/dev/null || echo 0) +if ping -c2 -W2 "$GW" >/dev/null 2>&1; then + [ "$n" -ge 3 ] && logger -t netwatch "네트워크 복구됨 (연속 실패 $n회 뒤)" + echo 0 > "$F"; exit 0 +fi +n=$((n+1)); echo "$n" > "$F" +logger -t netwatch "공유기 핑 실패 $n회 (wlan0: $(nmcli -t -f DEVICE,STATE dev | grep wlan0), usb: $(lsusb | grep -c 0bda:0179))" +if [ "$n" -eq 3 ]; then + logger -t netwatch "wlan0 재연결 시도" + nmcli device disconnect wlan0; sleep 2; nmcli device connect wlan0 +elif [ "$n" -eq 6 ]; then + logger -t netwatch "Wi-Fi 드라이버 재적재" + nmcli radio wifi off; modprobe -r rtl8xxxu; sleep 2; modprobe rtl8xxxu; sleep 3; nmcli radio wifi on +elif [ "$n" -ge 10 ]; then + up=$(cut -d. -f1 /proc/uptime) + if [ "$up" -gt 900 ]; then + logger -t netwatch "10분 넘게 복구 안 됨 — 재부팅"; sync; systemctl reboot + fi +fi diff --git a/devices/pi2-greenhouse/netwatch.timer b/devices/pi2-greenhouse/netwatch.timer new file mode 100644 index 0000000..9272e6a --- /dev/null +++ b/devices/pi2-greenhouse/netwatch.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Run network watchdog every minute +[Timer] +OnBootSec=5min +OnUnitActiveSec=1min +AccuracySec=10s +[Install] +WantedBy=timers.target