GPSd定时检测保活TCP GPS源

为了在 TCP GPS 源丢失连接时自动重新连接,可以编写一个监控脚本,定期检查 gpspipe 输出中的 TCP 源数据是否存在。如果检测到丢失,则使用 gpsdctl 或直接命令重新添加 TCP 源。

1、工具

检查并安装必要工具,本例需要使用 gpspipe

安装 gpspipe

shell 复制代码
sudo apt update
sudo apt install gpsd-clients

2、检测脚本

以下脚本每隔 10 秒检查一次 gpspipe 的输出,并在 TCP GPS 源断开时重新添加。

shell 复制代码
#!/bin/bash

# 配置 TCP 源地址和监控间隔
TCP_SOURCE="tcp://10.0.6.116:12321"
CHECK_INTERVAL=10  # 每 10 秒检查一次

# 检查 TCP GPS 源是否丢失
check_tcp_source() {
    # 检查 gpspipe 的实时输出是否包含 TCP_SOURCE
    if ! gpspipe -w -n 10 | grep -q "\"device\":\"$TCP_SOURCE\""; then
        echo "$(date): 检测到 TCP GPS 源丢失,尝试重新添加..."
        gpsdctl add "$TCP_SOURCE" || echo "$(date): 无法重新添加 TCP GPS 源,请检查网络或 GPSD 配置。"
    else
        echo "$(date): TCP GPS 源正常。"
    fi
}

# 主循环
while true; do
    check_tcp_source
    sleep "$CHECK_INTERVAL"
done

3、服务化

假设脚本路径上述脚本的路径是/etc/monitor_tcp_gps.sh

创建服务单元文件,服务单元文件需要放置在 /etc/systemd/system/ 目录下。

创建文件 /etc/systemd/system/monitor_tcp_gps.service

shell 复制代码
sudo nano /etc/systemd/system/monitor_tcp_gps.service
shell 复制代码
[Unit]
Description=Monitor and reconnect TCP GPS source for gpsd
After=network.target gpsd.service

[Service]
Type=simple
ExecStart=/etc/monitor_tcp_gps.sh
Restart=on-failure
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=monitor_tcp_gps
User=root
Environment=CONFIG_FILE=/etc/GPS_config.ini

[Install]
WantedBy=multi-user.target

配置说明

• After=network.target gpsd.service:确保网络和 gpsd 服务已启动。

• Restart=on-failure:服务在失败时自动重启。

• RestartSec=10:失败后等待 10 秒再重启服务。

• SyslogIdentifier=monitor_tcp_gps:日志输出到 syslog 中,标记为 monitor_tcp_gps,便于过滤。

确保脚本可执行:

xml 复制代码
sudo chmod +x /etc/RPI_APRS/monitor_tcp_gps.sh

重新加载 systemd 配置,配置开机启动服务

xml 复制代码
sudo systemctl daemon-reload
sudo systemctl enable monitor_tcp_gps.service
sudo systemctl start monitor_tcp_gps.service
sudo systemctl status monitor_tcp_gps.service
相关推荐
wl851120 分钟前
SAP CPI 教程003 如何抓取Http适配器异常信息
网络·网络协议·http
飞Link41 分钟前
【常见协议与服务】HTTP1.1、HTTP2、HTTP3:性能到底差在哪
网络·http
Johnstons1 小时前
网络性能分析怎么做:从时延、抖动、丢包到定位根因的实战判断框架
网络·ar·安全威胁分析·网络性能分析实战框架
feng14561 小时前
稳定性-资金安全和资损防控
运维·网络·安全
奇妙之二进制1 小时前
zmq源码分析之IO线程绑定时机
开发语言·网络
多年小白1 小时前
AI 日报 - 2026年4月25日(周六)
网络·人工智能·科技·深度学习·ai
Johnstons2 小时前
网络诊断工具怎么选:从监控告警到抓包定位的完整方法论
服务器·网络·php·es·抓包分析·网络诊断工具选型与排障方法
惊鸿若梦一书生2 小时前
《Python 高阶教程》016|偏函数与柯里化:把复杂调用拆成更简单的组合
linux·网络·python
lularible2 小时前
PTP协议精讲(3.7):传输层实现——PTP报文的“高速公路“
网络·网络协议·开源·嵌入式·ptp
LCG元2 小时前
STM32实战:基于STM32F407的LWIP以太网通信(TCP Server)
stm32·嵌入式硬件·tcp/ip