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
相关推荐
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
wuyk5551 天前
《WiFi 嵌入式物联网开发全套实战》| 第 16 章 ESP32 AP+STA 双模共存原理与工程坑点
网络·stm32·物联网
QYRdata1 天前
年均增速24.2%!机器人数据湖未来六年增长动能强劲
网络·机器人·服务发现
CHENKONG_CK1 天前
破解制鞋打磨痛点:RFID赋能去毛刺工序自动化升级
网络·单片机·嵌入式硬件·网络协议·tcp/ip
chshang19921 天前
工业路由器是什么?浅谈5G工业网络中的IR602
网络·物联网·5g·智能路由器
萧瑟余晖1 天前
Netty 核心组件与 Reactor 模型详解
网络·架构
ITxiaobing20231 天前
IP 定位服务选型指南:从准确率到工程落地的技术考察
linux·服务器·网络
wuyk5551 天前
【Socket 进阶之路】第 9 章 Linux 网络服务量产稳定性优化|心跳保活、TIME_WAIT、SO_LINGER、内存池、断线重连、完整异常防护框架
linux·服务器·开发语言·网络·物联网
z落落1 天前
C#UDP+串口服务端+UDP 客户端(含 CRC16 校验)
网络·网络协议·udp
云运维笔记1 天前
华为设备IP地址配置全攻略
运维·网络·计算机网络·华为