|
| 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