树莓派Jessie系统gpsd端口2947被init占用无法启动服务故障咨询
设备基础信息
正常工作Pi(WORKING PI)配置
- 型号:Model B Rev 2
- 系统:Jessie 4.1.19+ #858
- gpsd版本:3.11-3+deb8ul
- 升级说明:该设备从原版Wheezy发行版升级到当前Jessie版本,Wheezy下TL-WN725N无线网卡可正常使用,升级Jessie后无线功能失效。
故障Pi(non working pi)配置
- 型号:Model B Rev 2+
- 系统:Jessie 4.1.7+ #817
- gpsd版本:3.11-3
- 版本说明:为原版发行版,已更新到最新版本,TL-WN725N无线网卡可正常使用。
两台设备共同点
- 均搭载RPI GPS扩展板,使用U-Blox 6 GPS芯片组;
- 执行
gpsmon /dev/ttyAMA0命令时,均可正常输出预期的GPS NMEA报文; - 可通过u-blox驱动启动gpsmon命令,在NMEA和U-Blox格式的GPS数据间切换。
故障现象
最终目标是通过Python在两台树莓派上记录GPS数据,需运行gpsd socket,可通过cgps -s验证状态:该命令在正常工作Pi上运行正常,在故障Pi上运行失败。
两台Pi均使用以下命令启动gpsd服务:sudo gpsd /dev/ttyAMA0 -b -N -D 3 -n -F /var/run/gpsd.sock
正常工作Pi运行结果
正常工作Pi可正常接受该命令,cgps -s运行正常,执行sudo netstat -ltnp输出如下:
$ sudo netstat -ltnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 498/ser2net tcp 0 0 0.0.0.0:2000 0.0.0.0:* LISTEN 498/ser2net tcp 0 0 0.0.0.0:2001 0.0.0.0:* LISTEN 498/ser2net tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 452/sshd tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN 369/inetd tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 498/ser2net tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 769/exim4 tcp 0 0 0.0.0.0:3001 0.0.0.0:* LISTEN 498/ser2net tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 814/0 tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 20461/2 tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 6177/5 tcp 0 0 127.0.0.1:6013 0.0.0.0:* LISTEN 6236/7 tcp 0 0 127.0.0.1:2947 0.0.0.0:* LISTEN 8609/gpsd tcp6 0 0 :::22 :::* LISTEN 452/sshd tcp6 0 0 ::1:25 :::* LISTEN 769/exim4 tcp6 0 0 ::1:6010 :::* LISTEN 814/0 tcp6 0 0 ::1:6011 :::* LISTEN 20461/2 tcp6 0 0 ::1:6012 :::* LISTEN 6177/5 tcp6 0 0 ::1:6013 :::* LISTEN 6236/7 tcp6 0 0 ::1:2947 :::* LISTEN 8609/gpsd
符合预期:IPv4和IPv6的2947端口均被gpsd服务占用。
故障Pi运行结果
故障Pi执行上述启动命令时报错如下:
$ sudo gpsd /dev/ttyAMA0 -b -N D 3 -n -F /var/run/gpsd.sock gpsd:ERROR: can't bind to IPv4 port gpsd, Address already in use gpsd:ERROR: maybe gpsd is already running! gpsd:ERROR: can't bind to IPv6 port gpsd, Address already in use gpsd:ERROR: maybe gpsd is already running!
执行sudo netstat -ltnp输出如下:
$ sudo netstat -ltnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:2947 0.0.0.0:* LISTEN 1/init tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 564/ser2net tcp 0 0 0.0.0.0:2000 0.0.0.0:* LISTEN 564/ser2net tcp 0 0 0.0.0.0:2001 0.0.0.0:* LISTEN 564/ser2net tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 477/sshd tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 564/ser2net tcp 0 0 0.0.0.0:3001 0.0.0.0:* LISTEN 564/ser2net tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 882/0 tcp6 0 0 ::1:2947 :::* LISTEN 1/init tcp6 0 0 :::22 :::* LISTEN 477/sshd tcp6 0 0 ::1:6010 :::* LISTEN 882/0
可见IPv4和IPv6的2947端口均被init服务占用。
对比两台Pi的以下文件,内容完全一致:
/lib/systemd/system/gpsd.socket /lib/systemd/system/gpsd.service /etc/default/gpsd
核心疑问
- 两台Pi均运行Jessie 4.1版本(仅修订号不同),为什么正常工作Pi开机时init不会占用2947端口,而故障Pi会占用该端口导致gpsd无法绑定socket?
- 是否有方法可以设置故障Pi不占用2947端口,或者通过命令行让init释放该端口?
- 是否故障Pi的无线网卡占用了该端口,导致无无线功能的正常工作Pi可正常绑定gpsd端口,带无线的故障Pi无法绑定?
补充测试内容
故障Pi测试
重启后执行命令:
$ systemctl is-enabled gpsd.socket enabled $ systemctl is-enabled gpsd.service static
cgps -s或cgps仍无法正常运行,出现超时。
telnet连接2947端口结果:
$ sudo telnet localhost 2947 Trying ::1... Connected to localhost. Escape character is '^]'. {"class":"VERSION","release":"3.11","rev":"3.11-3","proto_major":3,"proto_minor":9} ?WATCH={"enable":true, "json"} {"class":"DEVICES","devices":[]} {"class":"WATCH","enable":true,"json":false,"nmea":false,"raw":0,"scaled":false,"timing":false,"split24":false,"pps":false} ^] telnet> quit Connection closed.
连接后无后续数据返回,出现挂起。gpsmon /dev/ttyAMA0仍可正常运行。
正常工作Pi测试
重启后执行命令:
$ systemctl is-enabled gpsd.socket disabled $ systemctl is-enabled gpsd.service static
用之前的命令启动服务结果:
$ sudo gpsd /dev/ttyAMA0 -b -N D 3 -n -F /var/run/gpsd.sock gpsd:ERROR: device open failed: No such file or directory - retrying read-only gpsd:ERROR: read-only device open failed: No such file or directory gpsd:ERROR: initial GPS device D open failed gpsd:ERROR: device open failed: No such file or directory - retrying read-only gpsd:ERROR: read-only device open failed: No such file or directory gpsd:ERROR: initial GPS device 3 open failed gpsd:ERROR: device open failed: No such file or directory - retrying read-only gpsd:ERROR: read-only device open failed: No such file or directory gpsd:ERROR: D: device activation failed. gpsd:ERROR: device open failed: No such file or directory - retrying read-only gpsd:ERROR: read-only device open failed: No such file or directory gpsd:ERROR: 3: device activation failed. gpsd:ERROR: device open failed: No such file or directory - retrying read-only gpsd:ERROR: read-only device open failed: No such file or directory gpsd:ERROR: D: device activation failed. gpsd:ERROR: device open failed: No such file or directory - retrying read-only gpsd:ERROR: read-only device open failed: No such file or directory gpsd:ERROR: 3: device activation failed. ^Z [1]+ Stopped sudo gpsd /dev/ttyAMA0 -b -N D 3 -n -F /var/run/gpsd.sock
执行cgps仅显示空白屏幕,此前会显示空表格。
执行gpsmon /dev/ttyAMA0报错如下:
$ gpsmon /dev/ttyAMA0 gpsmon:ERROR: device open failed: Device or resource busy - retrying read-only gpsmon:ERROR: read-only device open failed: Device or resource busy gpsmon: activation of device /dev/ttyAMA0 failed, errno=16 (Device or resource busy)
重启后gpsmon /dev/ttyAMA0可恢复正常运行。
补充说明
执行systemctl enable --now gpsd.socket时报错:systemctl: unrecognized option '--now'
总体观察:gpsmon /dev/ttyAMA0可正常运行是因为直接从串口读取NMEA报文输出到屏幕,而cgps或Python代码无法运行是因为socket或服务未正常工作。
疑问解答
- 端口占用差异和系统修订号无关,原因是故障Pi为原版Jessie系统,
gpsd.socket默认启用,systemd会预先占用2947端口等待请求,再唤醒gpsd进程,所以netstat会显示端口被PID1的init占用;正常工作Pi是从Wheezy升级到Jessie,升级过程中gpsd.socket默认保持禁用状态,所以不会占用端口。 - 直接执行对应命令即可永久释放2947端口,后续可以正常手动启动gpsd服务,不会再出现占用错误。
- 2947端口和无线网卡完全无关,无线网卡不会占用该端口,你看到的占用现象是systemd的socket激活机制导致的。
修复步骤
故障Pi修复步骤
- 停止并禁用gpsd.socket服务,释放2947端口:
sudo systemctl stop gpsd.socket sudo systemctl disable gpsd.socket
- 编辑
/etc/default/gpsd配置文件,指定GPS串口路径:
DEVICES="/dev/ttyAMA0" GPSD_OPTIONS="-b -n"
- 启动gpsd服务,可选择用systemd管理或者直接运行你之前的手动启动命令:
# 方式1:用systemd管理 sudo systemctl restart gpsd.service # 方式2:手动启动 sudo gpsd /dev/ttyAMA0 -b -N -D 3 -n -F /var/run/gpsd.sock
- 执行
cgps -s验证,可正常读取GPS数据即修复完成。
正常工作Pi修复步骤
你之前的启动命令输入错误,-D 3被误写为D 3,导致系统把D和3识别为设备路径才触发报错,修正命令即可正常启动:
sudo gpsd /dev/ttyAMA0 -b -N -D 3 -n -F /var/run/gpsd.sock
内容的提问来源于stack exchange,提问作者dprice1551

