Photo by Unsplash
697 字
3 分钟
OpenWrt AdGuardHome运行检测脚本开发
OpenWrt AdGuardHome 运行检测脚本开发
在软路由上跑多个服务时,最怕某个进程挂了没发现。这篇记录一下我写的检测脚本,可以同时监控 AdGuardHome、Homebox 和 Mihomo 三个服务,挂了自动拉起。
OpenWrt 环境特点
OpenWrt 用的是 BusyBox 提供的 ash,不是完整的 bash。写脚本时需要注意:
- 不能用
[[双括号,改用[单括号 pidof是最通用的进程检测方式- 路径要先检查是否存在再操作
完整检测脚本
#!/bin/sh
# ==========================================# 1. AdGuardHome 检测与启动# ==========================================if pidof AdGuardHome > /dev/null; then echo "AdGuardHome: Already running"else echo "AdGuardHome is not running. Starting..."
if [ -d "/data/AdGuardHome" ]; then cd /data/AdGuardHome ./AdGuardHome -s install
# 防火墙规则:将53端口重定向到AdGuardHome iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5625 iptables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 5625 ip6tables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5625 2>/dev/null ip6tables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 5625 2>/dev/null
echo "AdGuardHome started." else echo "Error: /data/AdGuardHome not found." fifi
echo "------------------------------------------"
# ==========================================# 2. Homebox 检测与启动# ==========================================if pidof homebox > /dev/null; then echo "Homebox: Already running"else echo "Homebox is not running. Starting..."
if [ -d "/data/homebox" ]; then cd /data/homebox # 后台静默运行,端口3080 ./homebox serve --port 3080 >/dev/null 2>&1 & echo "Homebox started." else echo "Error: /data/homebox not found." fifi
echo "------------------------------------------"
# ==========================================# 3. Mihomo 检测与启动# ==========================================MIHOMO_DIR="/mnt/usb-013165a8/mihomo"
if pidof mihomo > /dev/null; then echo "Mihomo: Already running"else echo "Mihomo is not running. Starting..."
if [ -d "$MIHOMO_DIR" ]; then cd "$MIHOMO_DIR" # 后台静默运行 ./mihomo -f config.yaml -d . >/dev/null 2>&1 & echo "Mihomo started." else echo "Error: Directory $MIHOMO_DIR not found." echo "Check if USB drive is mounted correctly." fifi关键点说明
进程检测
if pidof AdGuardHome > /dev/null; then # 进程存在fipidof 在 BusyBox 环境下很稳定,返回进程ID或空。
防火墙规则
AdGuardHome 监听 5625 端口,用 iptables 把标准 DNS 53 端口重定向过去:
iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5625规则持久化直接在命令行添加的 iptables 规则重启后失效。需要把这几行加入
/etc/firewall.user或 LuCI 的自定义规则中。
后台运行
./homebox serve --port 3080 >/dev/null 2>&1 &&- 后台运行>/dev/null 2>&1- 丢弃输出,防止僵尸进程
USB 挂载点
Mihomo 放在 USB 硬盘上(/mnt/usb-013165a8/mihomo)。需要注意:
- 开机时 USB 可能还没挂载完成
- 如果通过 crontab 定时执行,第一次失败没关系,下次会成功
- 如果只在开机运行一次,脚本开头加
sleep 10等待挂载
定时任务配置
在 LuCI 界面或命令行添加 crontab:
# 每5分钟检查一次*/5 * * * * /bin/sh /etc/check_services.sh或者开机运行:
# 编辑 /etc/rc.local,在 exit 0 之前添加sleep 10/bin/sh /etc/check_services.sh使用说明
保存脚本后添加执行权限:
chmod +x /etc/check_services.sh手动测试:
/bin/sh /etc/check_services.sh参考链接
- AdGuardHome 官方文档:https://github.com/AdguardTeam/AdGuardHome
- Homebox 项目地址:https://github.com/prosegrinder/homebox
- Mihomo (Clash.Meta):https://github.com/MetaCubeX/mihomo
OpenWrt AdGuardHome运行检测脚本开发
https://im.awsl.app/posts/networking/034-openwrt-adguardhome/