Skip to content

Commit 97b9320

Browse files
committed
feat(system): add service status command
1 parent 478c1c1 commit 97b9320

5 files changed

Lines changed: 265 additions & 14 deletions

File tree

README.en.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<p align="left">
44
<a href="README.md"><img src="https://img.shields.io/badge/切换语言-简体中文-blue" alt="简体中文"></a>
55
<a href="README.en.md"><img src="https://img.shields.io/badge/Switch-English-blue" alt="English"></a>
6-
<a href="https://github.com/lizuju/ros2-web-desktop/releases/latest"><img src="https://img.shields.io/badge/Release-v0.1.5-green" alt="Release"></a>
6+
<a href="https://github.com/lizuju/ros2-web-desktop/releases/latest"><img src="https://img.shields.io/badge/Release-v0.1.6-green" alt="Release"></a>
77
<img src="https://img.shields.io/badge/No-X11%20Forwarding-orange" alt="No X11 Forwarding">
88
</p>
99

@@ -60,7 +60,7 @@ Windows users can use PowerShell or Windows Terminal. If `ssh` is missing, enabl
6060
For normal users, download the Release archive. Git is not required:
6161

6262
```bash
63-
wget https://github.com/lizuju/ros2-web-desktop/releases/download/v0.1.5/ros2-web-desktop.tar.gz
63+
wget https://github.com/lizuju/ros2-web-desktop/releases/download/v0.1.6/ros2-web-desktop.tar.gz
6464
tar -xzf ros2-web-desktop.tar.gz
6565
cd ros2-web-desktop
6666
./scripts/setup-ros2-novnc-system.sh
@@ -82,7 +82,7 @@ The setup script will:
8282
- suggest uncommon free ports, usually starting from `31880` and `31901`
8383
- validate port values and avoid occupied/conflicting ports
8484
- install noVNC, Xvfb, x11vnc, fluxbox, xterm, and related dependencies
85-
- print the doctor, start, and SSH tunnel commands
85+
- print the doctor, status, start, and SSH tunnel commands
8686

8787
Prompt guidance:
8888

@@ -113,6 +113,12 @@ cd ~/ros2-web-desktop
113113

114114
`doctor` only checks ROS 2, dependencies, ports, DISPLAY, and noVNC status. It does not start `rviz2` and does not add GPU load on the remote device. If you see `FAIL`, follow the printed suggestion. `WARN` usually means the service has not been started yet.
115115

116+
Check whether noVNC / x11vnc / Xvfb are currently running:
117+
118+
```bash
119+
./scripts/status-ros2-novnc-system.sh
120+
```
121+
116122
### 3. Start on the Remote Device
117123

118124
```bash
@@ -300,7 +306,7 @@ cd ~/ros2-web-desktop
300306
./scripts/stop-ros2-novnc-system.sh
301307
```
302308

303-
The script checks only this project's `websockify`, `x11vnc`, and related processes on the configured `NOVNC_PORT` / `VNC_PORT`, so it does not blindly kill unrelated services.
309+
The script checks only this project's pid file and matching `websockify`, `x11vnc`, and related processes on the configured `NOVNC_PORT` / `VNC_PORT`, so it does not blindly kill unrelated services. Run `./scripts/status-ros2-novnc-system.sh` before or after stopping to check the current state.
304310

305311
## Troubleshooting
306312

@@ -312,10 +318,11 @@ X11 forwarding can lag with RViz2, point clouds, maps, and cross-platform setups
312318

313319
```bash
314320
cd ~/ros2-web-desktop
321+
./scripts/status-ros2-novnc-system.sh
315322
./scripts/doctor-ros2-novnc-system.sh
316323
```
317324

318-
This is usually caused by a stopped remote service, a port conflict, or a missing SSH tunnel. Check `doctor`, then restart `./scripts/start-ros2-novnc-system.sh`.
325+
This is usually caused by a stopped remote service, a port conflict, or a missing SSH tunnel. Check `status` first to see whether noVNC / x11vnc / Xvfb are running, then check `doctor` and restart `./scripts/start-ros2-novnc-system.sh`.
319326

320327
**How should I choose ports?**
321328

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<p align="left">
44
<a href="README.md"><img src="https://img.shields.io/badge/切换语言-简体中文-blue" alt="简体中文"></a>
55
<a href="README.en.md"><img src="https://img.shields.io/badge/Switch-English-blue" alt="English"></a>
6-
<a href="https://github.com/lizuju/ros2-web-desktop/releases/latest"><img src="https://img.shields.io/badge/Release-v0.1.5-green" alt="Release"></a>
6+
<a href="https://github.com/lizuju/ros2-web-desktop/releases/latest"><img src="https://img.shields.io/badge/Release-v0.1.6-green" alt="Release"></a>
77
<img src="https://img.shields.io/badge/No-X11%20Forwarding-orange" alt="No X11 Forwarding">
88
</p>
99

@@ -60,7 +60,7 @@ Windows 用户可以使用 PowerShell 或 Windows Terminal。若系统没有 `ss
6060
普通用户可以直接下载 Release 压缩包,不需要安装 Git:
6161

6262
```bash
63-
wget https://github.com/lizuju/ros2-web-desktop/releases/download/v0.1.5/ros2-web-desktop.tar.gz
63+
wget https://github.com/lizuju/ros2-web-desktop/releases/download/v0.1.6/ros2-web-desktop.tar.gz
6464
tar -xzf ros2-web-desktop.tar.gz
6565
cd ros2-web-desktop
6666
./scripts/setup-ros2-novnc-system.sh
@@ -82,7 +82,7 @@ cd ros2-web-desktop
8282
- 自动推荐不常见且未占用的端口,通常从 `31880``31901` 开始找
8383
- 检查端口是否有效、是否被占用、两个端口是否冲突
8484
- 安装 noVNC、Xvfb、x11vnc、fluxbox、xterm 等依赖
85-
- 最后输出自检、启动和 SSH 隧道命令
85+
- 最后输出自检、状态、启动和 SSH 隧道命令
8686

8787
提示项说明:
8888

@@ -113,6 +113,12 @@ cd ~/ros2-web-desktop
113113

114114
`doctor` 只检查 ROS 2、依赖、端口、DISPLAY 和 noVNC 状态,不启动 `rviz2`,也不会增加远程设备的 GPU 负载。看到 `FAIL` 时,按输出的建议处理;只有 `WARN` 通常表示服务还没启动。
115115

116+
查看 noVNC / x11vnc / Xvfb 当前是否运行:
117+
118+
```bash
119+
./scripts/status-ros2-novnc-system.sh
120+
```
121+
116122
### 3. 远程设备每次启动
117123

118124
```bash
@@ -300,7 +306,7 @@ cd ~/ros2-web-desktop
300306
./scripts/stop-ros2-novnc-system.sh
301307
```
302308

303-
这个脚本只会按 `.env` 里的 `NOVNC_PORT` / `VNC_PORT` 检查本项目的 `websockify``x11vnc` 等进程,不会按端口号全局乱杀其它服务。
309+
这个脚本只会按 `.env` 里的 `NOVNC_PORT` / `VNC_PORT` 和本项目 pid 文件检查 `websockify``x11vnc` 等进程,不会按端口号全局乱杀其它服务。停止前后可以用 `./scripts/status-ros2-novnc-system.sh` 看当前状态
304310

305311
## 常见问题
306312

@@ -312,10 +318,11 @@ X11 转发在 RViz2、点云、地图这类图形界面上容易卡,尤其是
312318

313319
```bash
314320
cd ~/ros2-web-desktop
321+
./scripts/status-ros2-novnc-system.sh
315322
./scripts/doctor-ros2-novnc-system.sh
316323
```
317324

318-
通常是远程服务没启动、端口冲突,或本地 SSH 隧道没开。先看 `doctor` 输出,再重启 `./scripts/start-ros2-novnc-system.sh`
325+
通常是远程服务没启动、端口冲突,或本地 SSH 隧道没开。先用 `status` 看 noVNC / x11vnc / Xvfb 是否运行,再看 `doctor` 输出并重启 `./scripts/start-ros2-novnc-system.sh`
319326

320327
**端口要怎么选?**
321328

scripts/setup-ros2-novnc-system.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ echo " VNC_PORT=${vnc_port}"
114114
echo "Check the remote ROS 2 desktop with:"
115115
echo " cd ${PROJECT_DIR}"
116116
echo " ./scripts/doctor-ros2-novnc-system.sh"
117+
echo "Show current service status with:"
118+
echo " ./scripts/status-ros2-novnc-system.sh"
117119
echo "Start the remote ROS 2 desktop with:"
118120
echo " cd ${PROJECT_DIR}"
119121
echo " ./scripts/start-ros2-novnc-system.sh"
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#!/usr/bin/env bash
2+
set -u
3+
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
PROJECT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
6+
7+
if [ -f "${PROJECT_DIR}/.env" ]; then
8+
set -a
9+
source "${PROJECT_DIR}/.env"
10+
set +a
11+
fi
12+
13+
DISPLAY="${DISPLAY:-:99}"
14+
NOVNC_LISTEN_HOST="${NOVNC_LISTEN_HOST:-127.0.0.1}"
15+
NOVNC_PORT="${NOVNC_PORT:-31880}"
16+
VNC_PORT="${VNC_PORT:-31901}"
17+
PID_FILE="${PROJECT_DIR}/logs/ros2-novnc.pids"
18+
19+
failures=0
20+
warnings=0
21+
22+
pass() {
23+
echo "PASS $1"
24+
}
25+
26+
warn() {
27+
warnings=$((warnings + 1))
28+
echo "WARN $1"
29+
}
30+
31+
fail() {
32+
failures=$((failures + 1))
33+
echo "FAIL $1"
34+
}
35+
36+
pid_args() {
37+
ps -p "$1" -o args= 2>/dev/null || true
38+
}
39+
40+
pid_exists() {
41+
ps -p "$1" >/dev/null 2>&1
42+
}
43+
44+
matches_expected_process() {
45+
local name="$1"
46+
local pid="$2"
47+
local args
48+
49+
args="$(pid_args "$pid")"
50+
case "$name" in
51+
Xvfb)
52+
[[ "$args" == *"Xvfb ${DISPLAY}"* ]]
53+
;;
54+
fluxbox)
55+
[[ "$args" == *"fluxbox"* ]]
56+
;;
57+
x11vnc)
58+
[[ "$args" == *"x11vnc"* && "$args" == *"-rfbport ${VNC_PORT}"* ]]
59+
;;
60+
websockify)
61+
[[ "$args" == *"websockify"* && "$args" == *":${NOVNC_PORT}"* && "$args" == *"127.0.0.1:${VNC_PORT}"* ]]
62+
;;
63+
xterm)
64+
[[ "$args" == *"xterm"* && "$args" == *"ROS 2 Remote Desktop"* ]]
65+
;;
66+
*)
67+
return 1
68+
;;
69+
esac
70+
}
71+
72+
pid_from_file() {
73+
local name="$1"
74+
75+
[ -f "$PID_FILE" ] || return
76+
awk -v name="$name" '$1 == name { print $2; exit }' "$PID_FILE"
77+
}
78+
79+
pids_for_port() {
80+
local port="$1"
81+
82+
ss -lntp 2>/dev/null \
83+
| awk -v port=":${port}" '$4 ~ port "$" { print }' \
84+
| sed -n 's/.*pid=\([0-9][0-9]*\).*/\1/p' \
85+
| sort -u
86+
}
87+
88+
check_recorded_component() {
89+
local label="$1"
90+
local name="$2"
91+
local detail="$3"
92+
local pid
93+
94+
pid="$(pid_from_file "$name" || true)"
95+
if [ -z "$pid" ]; then
96+
warn "${label}: not confirmed; pid file has no ${name} entry"
97+
return
98+
fi
99+
100+
if ! [[ "$pid" =~ ^[0-9]+$ ]]; then
101+
warn "${label}: invalid pid entry in ${PID_FILE}: ${pid}"
102+
return
103+
fi
104+
105+
if ! pid_exists "$pid"; then
106+
warn "${label}: not running; recorded pid ${pid} is gone"
107+
return
108+
fi
109+
110+
if matches_expected_process "$name" "$pid"; then
111+
pass "${label}: running (pid ${pid}${detail})"
112+
else
113+
warn "${label}: pid ${pid} is running but does not match this project"
114+
echo " $(pid_args "$pid")"
115+
fi
116+
}
117+
118+
check_port_component() {
119+
local label="$1"
120+
local name="$2"
121+
local port="$3"
122+
local detail="$4"
123+
local found=0
124+
local matched=0
125+
local pid
126+
127+
for pid in $(pids_for_port "$port"); do
128+
found=1
129+
if matches_expected_process "$name" "$pid"; then
130+
matched=1
131+
pass "${label}: running (pid ${pid}${detail})"
132+
else
133+
fail "${label}: port ${port} is occupied by another process: pid ${pid}"
134+
echo " $(pid_args "$pid")"
135+
fi
136+
done
137+
138+
if [ "$found" -eq 0 ]; then
139+
warn "${label}: not listening on port ${port}"
140+
elif [ "$matched" -eq 0 ]; then
141+
echo " Run ./scripts/stop-ros2-novnc-system.sh if this is an old ros2-web-desktop process, or change the port in .env."
142+
fi
143+
}
144+
145+
echo "ros2-web-desktop status"
146+
echo "Project: ${PROJECT_DIR}"
147+
echo "Config: DISPLAY=${DISPLAY}, NOVNC_LISTEN_HOST=${NOVNC_LISTEN_HOST}, NOVNC_PORT=${NOVNC_PORT}, VNC_PORT=${VNC_PORT}"
148+
if [ -f "$PID_FILE" ]; then
149+
echo "PID file: ${PID_FILE}"
150+
else
151+
echo "PID file: missing (${PID_FILE})"
152+
fi
153+
echo
154+
155+
check_recorded_component "Xvfb" Xvfb ", DISPLAY=${DISPLAY}"
156+
check_recorded_component "fluxbox" fluxbox ""
157+
check_port_component "x11vnc" x11vnc "$VNC_PORT" ", VNC_PORT=${VNC_PORT}"
158+
check_port_component "noVNC/websockify" websockify "$NOVNC_PORT" ", http://${NOVNC_LISTEN_HOST}:${NOVNC_PORT}/vnc.html"
159+
check_recorded_component "xterm" xterm ""
160+
echo
161+
162+
if [ "$failures" -gt 0 ]; then
163+
echo "Status found ${failures} failure(s) and ${warnings} warning(s)."
164+
echo "Next steps:"
165+
echo " ./scripts/doctor-ros2-novnc-system.sh"
166+
echo " ./scripts/stop-ros2-novnc-system.sh"
167+
echo " ./scripts/start-ros2-novnc-system.sh"
168+
exit 1
169+
fi
170+
171+
if [ "$warnings" -gt 0 ]; then
172+
echo "Status found ${warnings} warning(s). If the service is not running yet, start it with:"
173+
echo " ./scripts/start-ros2-novnc-system.sh"
174+
else
175+
echo "All tracked ros2-web-desktop components are running."
176+
fi

0 commit comments

Comments
 (0)