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
相关推荐
EasyDSS33 分钟前
国标GB28181设备管理软件EasyGBS远程视频监控方案助力高效安全运营
网络·人工智能
玩转4G物联网35 分钟前
零基础玩转物联网-串口转以太网模块如何快速实现与TCP服务器通信
服务器·网络·物联网·网络协议·tcp/ip·http·fs100p
派阿喵搞电子1 小时前
Ubuntu下有关UDP网络通信的指令
linux·服务器·网络
搬码临时工2 小时前
外网访问内网服务器常用的三种简单操作步骤方法,本地搭建网址轻松让公网连接
服务器·网络·智能路由器
Fortinet_CHINA2 小时前
引领AI安全新时代 Accelerate 2025北亚巡展·北京站成功举办
网络·安全
dustcell.3 小时前
Cisco Packer Tracer 综合实验
网络
量子-Alex5 小时前
【反无人机检测】C2FDrone:基于视觉Transformer网络的无人机间由粗到细检测
网络·transformer·无人机
Jeremy_Lee1236 小时前
grafana 批量视图备份及恢复(含数据源)
前端·网络·grafana
洛神灬殇6 小时前
【LLM大模型技术专题】「入门到精通系列教程」基于ai-openai-spring-boot-starter集成开发实战指南
网络·数据库·微服务·云原生·架构
上海云盾第一敬业销售6 小时前
高防IP可以防护什么攻击类型?企业网络安全的第一道防线
网络·tcp/ip·web安全