From 6dfec24d66e90a03e86b1ddd3bf8479e5ae44c65 Mon Sep 17 00:00:00 2001 From: "Mr.Xia" <1424473282@qq.com> Date: Fri, 27 Feb 2026 13:52:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drunsh=20=E7=9A=84=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.sh | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/run.sh b/run.sh index 61f7eaa..ad81d94 100644 --- a/run.sh +++ b/run.sh @@ -10,13 +10,27 @@ NC='\033[0m' PID_FILE="app.pid" LOG_FILE="app.log" +is_port_listening() { + if command -v lsof >/dev/null 2>&1; then + lsof -iTCP:2333 -sTCP:LISTEN >/dev/null 2>&1 + elif command -v ss >/dev/null 2>&1; then + ss -ltn 2>/dev/null | awk '{print $4}' | grep -Eq '(^|[[:space:]])([^[:space:]]*:)?2333$' + elif command -v netstat >/dev/null 2>&1; then + netstat -ltn 2>/dev/null | awk '{print $4}' | grep -Eq '(^|[[:space:]])([^[:space:]]*:)?2333$' + elif command -v fuser >/dev/null 2>&1; then + fuser -n tcp 2333 >/dev/null 2>&1 + else + return 1 + fi +} + get_port_pids() { if command -v lsof >/dev/null 2>&1; then lsof -t -i:2333 2>/dev/null elif command -v ss >/dev/null 2>&1; then - ss -ltnp 'sport = :2333' 2>/dev/null | sed -n 's/.*pid=\([0-9]\+\).*/\1/p' | sort -u + ss -ltnp 2>/dev/null | grep -E '[:.]2333[[:space:]]' | sed -n 's/.*pid=\([0-9]\+\).*/\1/p' | sort -u elif command -v netstat >/dev/null 2>&1; then - netstat -ltnp 2>/dev/null | sed -n 's/.* \([0-9]\+\)\/.*/\1/p' | sort -u + netstat -ltnp 2>/dev/null | grep -E '[:.]2333[[:space:]]' | sed -n 's/.* \([0-9]\+\)\/.*/\1/p' | sort -u elif command -v fuser >/dev/null 2>&1; then fuser -n tcp 2333 2>/dev/null | tr ' ' '\n' | sort -u fi @@ -81,13 +95,11 @@ function do_start() { return 0 fi - PORT_PID=$(get_port_pids) - if [ -n "$PORT_PID" ]; then + if is_port_listening; then printf "${YELLOW}Port 2333 is in use, trying to stop old process...${NC}\n" do_stop sleep 1 - PORT_PID=$(get_port_pids) - if [ -n "$PORT_PID" ]; then + if is_port_listening; then printf "${RED}Error: port 2333 still in use after auto-stop${NC}\n" return 1 fi @@ -154,7 +166,7 @@ case "$1" in [ -f "$LOG_FILE" ] && tail -f "$LOG_FILE" || printf "${RED}Log file not found${NC}\n" ;; status) - if [ -n "$(get_port_pids)" ]; then + if is_port_listening; then printf "${GREEN}App is running${NC}\n" (command -v lsof >/dev/null 2>&1 && lsof -i:2333) || (command -v ss >/dev/null 2>&1 && ss -ltnp 'sport = :2333') || (command -v netstat >/dev/null 2>&1 && netstat -ltnp) || true else @@ -174,4 +186,3 @@ case "$1" in esac -