diff --git a/run.sh b/run.sh index 56a6f0c..d1ecf07 100644 --- a/run.sh +++ b/run.sh @@ -10,6 +10,18 @@ NC='\033[0m' PID_FILE="app.pid" LOG_FILE="app.log" +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 | awk -F'pid=' 'NR>1 {split($2,a,",",); print a[1]}' + elif command -v netstat >/dev/null 2>&1; then + netstat -ltnp 2>/dev/null | awk '$4 ~ /:2333$/ {split($7,a,"/"); print a[1]}' + elif command -v fuser >/dev/null 2>&1; then + fuser -n tcp 2333 2>/dev/null + fi +} + # --- 逻辑封装 --- function do_stop() { @@ -24,7 +36,7 @@ function do_stop() { fi # 2. 强力兜底:查找并杀死占?2333 端口的所?Node 进程 - PORT_PIDS=$(lsof -t -i:2333) + PORT_PIDS=$(get_port_pids) if [ -n "$PORT_PIDS" ]; then for P in $PORT_PIDS; do kill -9 $P >/dev/null 2>&1 @@ -39,12 +51,12 @@ function do_start() { return 0 fi - PORT_PID=$(lsof -t -i:2333) + PORT_PID=$(get_port_pids) if [ -n "$PORT_PID" ]; then printf "${YELLOW}?? 2333 ??????????????...${NC}\n" do_stop sleep 1 - PORT_PID=$(lsof -t -i:2333) + PORT_PID=$(get_port_pids) if [ -n "$PORT_PID" ]; then printf "${RED}??: ?? 2333 ?????????????????${NC}\n" return 1 @@ -99,9 +111,9 @@ case "$1" in [ -f "$LOG_FILE" ] && tail -f "$LOG_FILE" || printf "${RED}日志文件不存?{NC}\n" ;; status) - if lsof -i:2333 >/dev/null 2>&1; then + if [ -n "$(get_port_pids)" ]; then printf "${GREEN}应用正在运行${NC}\n" - lsof -i:2333 + (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 printf "${RED}应用未运?{NC}\n" fi