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
相关推荐
七七&5566 小时前
2024年08月13日 Go生态洞察:Go 1.23 发布与全面深度解读
开发语言·网络·golang
元清加油6 小时前
【Golang】:函数和包
服务器·开发语言·网络·后端·网络协议·golang
向日葵.8 小时前
fastdds.ignore_local_endpoints 属性
服务器·网络·php
athink_cn10 小时前
HTTP/2新型漏洞“MadeYouReset“曝光:可发动大规模DoS攻击
网络·网络协议·安全·http·网络安全
zzc92111 小时前
TLSv1.2协议与TCP/UDP协议传输数据内容差异
网络·测试工具·安全·wireshark·ssl·密钥·tlsv1.2
Peter_Deng.11 小时前
Linux 下基于 TCP 的 C 语言客户端/服务器通信详解(三个示例逐步进阶)
服务器·c语言·网络
wxy31912 小时前
嵌入式LINUX——————TCP并发服务器
java·linux·网络
蒋星熠13 小时前
C++零拷贝网络编程实战:从理论到生产环境的性能优化之路
网络·c++·人工智能·深度学习·性能优化·系统架构
huluang13 小时前
医院网络安全重保行动方案
网络·安全
九州ip动态13 小时前
如何安全使用改IP软件更改异地IP地址?
网络·tcp/ip·安全