diff --git a/luasrc/controller/openclaw.lua b/luasrc/controller/openclaw.lua index 6b7acf5..bd1fd86 100644 --- a/luasrc/controller/openclaw.lua +++ b/luasrc/controller/openclaw.lua @@ -122,19 +122,20 @@ function action_status() end -- 网关端口检查 - local gw_check = sys.exec("netstat -tlnp 2>/dev/null | grep -c ':" .. port .. " ' || echo 0"):gsub("%s+", "") + local gw_check_cmd = "if command -v ss >/dev/null 2>&1; then ss -tulnp 2>/dev/null | grep -c ':" .. port .. " ' || echo 0; else netstat -tulnp 2>/dev/null | grep -c ':" .. port .. " ' || echo 0; fi" + local gw_check = sys.exec(gw_check_cmd):gsub("%s+", "") result.gateway_running = (tonumber(gw_check) or 0) > 0 -- 如果端口未监听但 procd 进程存在,说明正在启动中 (gateway 初始化需要数分钟) if not result.gateway_running and enabled == "1" then - local procd_pid = sys.exec("pgrep -f 'openclaw.*gateway' 2>/dev/null | head -1"):gsub("%s+", "") + local procd_pid = sys.exec("pgrep -f 'openclaw.*gateway|node.*openclaw.*gateway' 2>/dev/null | head -1"):gsub("%s+", "") if procd_pid ~= "" then result.gateway_starting = true end end -- PTY 端口检查 - local pty_check = sys.exec("netstat -tlnp 2>/dev/null | grep -c ':" .. pty_port .. " ' || echo 0"):gsub("%s+", "") + local pty_check = sys.exec("netstat -tulnp 2>/dev/null | grep -c ':" .. pty_port .. " ' || echo 0"):gsub("%s+", "") result.pty_running = (tonumber(pty_check) or 0) > 0 -- 读取当前活跃模型 @@ -173,7 +174,7 @@ function action_status() -- PID 和内存 if result.gateway_running then - local pid = sys.exec("netstat -tlnp 2>/dev/null | awk '/:" .. port .. " /{split($NF,a,\"/\");print a[1];exit}'"):gsub("%s+", "") + local pid = sys.exec("netstat -tulnp 2>/dev/null | awk '/:" .. port .. " /{split($NF,a,\"/\");print a[1];exit}'"):gsub("%s+", "") if pid and pid ~= "" then result.pid = pid -- 内存 (VmRSS from /proc) diff --git a/root/etc/init.d/openclaw b/root/etc/init.d/openclaw index b1a4130..da2a098 100755 --- a/root/etc/init.d/openclaw +++ b/root/etc/init.d/openclaw @@ -213,9 +213,9 @@ _ensure_port_free() { local p="$1" max_wait="${2:-5}" i=0 while [ $i -lt $max_wait ]; do if command -v ss >/dev/null 2>&1; then - ss -tlnp 2>/dev/null | grep -q ":${p} " || return 0 + ss -tulnp 2>/dev/null | grep -q ":${p} " || return 0 else - netstat -tlnp 2>/dev/null | grep -q ":${p} " || return 0 + netstat -tulnp 2>/dev/null | grep -q ":${p} " || return 0 fi # 尝试杀掉占用端口的 gateway 进程 if [ $i -eq 0 ]; then @@ -230,9 +230,9 @@ _ensure_port_free() { # 最后手段: SIGKILL local port_pid="" if command -v ss >/dev/null 2>&1; then - port_pid=$(ss -tlnp 2>/dev/null | grep ":${p} " | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | head -1) + port_pid=$(ss -tulnp 2>/dev/null | grep ":${p} " | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | head -1) else - port_pid=$(netstat -tlnp 2>/dev/null | grep ":${p} " | sed -n 's|.* \([0-9]*\)/.*|\1|p' | head -1) + port_pid=$(netstat -tulnp 2>/dev/null | grep ":${p} " | sed -n 's|.* \([0-9]*\)/.*|\1|p' | head -1) fi [ -n "$port_pid" ] && kill -9 "$port_pid" 2>/dev/null && sleep 1 return 0 @@ -313,9 +313,9 @@ done local wait_count=0 while [ $wait_count -lt 8 ]; do if command -v ss >/dev/null 2>&1; then - ss -tlnp 2>/dev/null | grep -q ":${port} " || break + ss -tulnp 2>/dev/null | grep -q ":${port} " || break else - netstat -tlnp 2>/dev/null | grep -q ":${port} " || break + netstat -tulnp 2>/dev/null | grep -q ":${port} " || break fi sleep 1 wait_count=$((wait_count + 1)) @@ -325,9 +325,9 @@ done if [ $wait_count -ge 8 ]; then local port_pid="" if command -v ss >/dev/null 2>&1; then - port_pid=$(ss -tlnp 2>/dev/null | grep ":${port} " | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | head -1) + port_pid=$(ss -tulnp 2>/dev/null | grep ":${port} " | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | head -1) else - port_pid=$(netstat -tlnp 2>/dev/null | grep ":${port} " | sed -n 's|.* \([0-9]*\)/.*|\1|p' | head -1) + port_pid=$(netstat -tulnp 2>/dev/null | grep ":${port} " | sed -n 's|.* \([0-9]*\)/.*|\1|p' | head -1) fi [ -n "$port_pid" ] && kill -9 "$port_pid" 2>/dev/null sleep 1 @@ -336,6 +336,7 @@ fi service_triggers() { procd_add_reload_trigger "openclaw" + procd_add_network_trigger "lan" "wan" } reload_service() { @@ -362,9 +363,9 @@ port=$(uci -q get openclaw.main.port || echo "18789") # 必须先杀子进程释放端口,否则 procd respawn 的新实例会因端口冲突而崩溃 local port_pid="" if command -v ss >/dev/null 2>&1; then - port_pid=$(ss -tlnp 2>/dev/null | grep ":${port} " | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | head -1) + port_pid=$(ss -tulnp 2>/dev/null | grep ":${port} " | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | head -1) else - port_pid=$(netstat -tlnp 2>/dev/null | grep ":${port} " | sed -n 's|.* \([0-9]*\)/.*|\1|p' | head -1) + port_pid=$(netstat -tulnp 2>/dev/null | grep ":${port} " | sed -n 's|.* \([0-9]*\)/.*|\1|p' | head -1) fi [ -n "$port_pid" ] && kill "$port_pid" 2>/dev/null @@ -389,9 +390,9 @@ done local wait_count=0 while [ $wait_count -lt 5 ]; do if command -v ss >/dev/null 2>&1; then - ss -tlnp 2>/dev/null | grep -q ":${port} " || break + ss -tulnp 2>/dev/null | grep -q ":${port} " || break else - netstat -tlnp 2>/dev/null | grep -q ":${port} " || break + netstat -tulnp 2>/dev/null | grep -q ":${port} " || break fi sleep 1 wait_count=$((wait_count + 1)) @@ -433,18 +434,18 @@ fi _check_port() { local p="$1" if command -v ss >/dev/null 2>&1; then - ss -tlnp 2>/dev/null | grep -q ":${p} " + ss -tulnp 2>/dev/null | grep -q ":${p} " else - netstat -tlnp 2>/dev/null | grep -q ":${p} " + netstat -tulnp 2>/dev/null | grep -q ":${p} " fi } _get_pid_by_port() { local p="$1" if command -v ss >/dev/null 2>&1; then - ss -tlnp 2>/dev/null | grep ":${p} " | head -1 | sed -n 's/.*pid=\([0-9]*\).*/\1/p' + ss -tulnp 2>/dev/null | grep ":${p} " | head -1 | sed -n 's/.*pid=\([0-9]*\).*/\1/p' else - netstat -tlnp 2>/dev/null | grep ":${p} " | head -1 | sed 's|.* \([0-9]*\)/.*|\1|' + netstat -tulnp 2>/dev/null | grep ":${p} " | head -1 | sed 's|.* \([0-9]*\)/.*|\1|' fi } diff --git a/scripts/build_offline_run.sh b/scripts/build_offline_run.sh index 058291d..1a725a0 100755 --- a/scripts/build_offline_run.sh +++ b/scripts/build_offline_run.sh @@ -226,7 +226,7 @@ if [ -x /etc/init.d/openclaw ]; then # 等待进程退出和端口释放 sleep 2 # 确保 gateway 子进程也已退出 - for pid in $(pgrep -f "openclaw-gateway|openclaw" 2>/dev/null); do + for pid in $(pgrep -f "node.*openclaw|openclaw.*gateway" 2>/dev/null); do kill "$pid" 2>/dev/null done sleep 1