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
相关推荐
DianSan_ERP5 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
gihigo19985 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
2501_946205525 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人
linux kernel5 天前
第七部分:高级IO
服务器·网络
数字护盾(和中)5 天前
BAS+ATT&CK:企业主动防御的黄金组合
服务器·网络·数据库
~远在太平洋~5 天前
Debian系统如何删除多余的kernel
linux·网络·debian
龙仔7255 天前
在麒麟V10服务器安全加固,sshd防暴力破解加固,实现“密码错误3次封IP”的需求
服务器·tcp/ip·安全
unfeeling_5 天前
Keepalived实验
linux·服务器·网络
坐吃山猪5 天前
OpenClaw04_Gateway常见问题
网络·gateway·openclaw