修复runsh 的状态获取
Some checks failed
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled

This commit is contained in:
2026-02-27 13:52:32 +08:00
parent 2543e79571
commit 6dfec24d66

27
run.sh
View File

@@ -10,13 +10,27 @@ NC='\033[0m'
PID_FILE="app.pid" PID_FILE="app.pid"
LOG_FILE="app.log" 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() { get_port_pids() {
if command -v lsof >/dev/null 2>&1; then if command -v lsof >/dev/null 2>&1; then
lsof -t -i:2333 2>/dev/null lsof -t -i:2333 2>/dev/null
elif command -v ss >/dev/null 2>&1; then 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 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 elif command -v fuser >/dev/null 2>&1; then
fuser -n tcp 2333 2>/dev/null | tr ' ' '\n' | sort -u fuser -n tcp 2333 2>/dev/null | tr ' ' '\n' | sort -u
fi fi
@@ -81,13 +95,11 @@ function do_start() {
return 0 return 0
fi fi
PORT_PID=$(get_port_pids) if is_port_listening; then
if [ -n "$PORT_PID" ]; then
printf "${YELLOW}Port 2333 is in use, trying to stop old process...${NC}\n" printf "${YELLOW}Port 2333 is in use, trying to stop old process...${NC}\n"
do_stop do_stop
sleep 1 sleep 1
PORT_PID=$(get_port_pids) if is_port_listening; then
if [ -n "$PORT_PID" ]; then
printf "${RED}Error: port 2333 still in use after auto-stop${NC}\n" printf "${RED}Error: port 2333 still in use after auto-stop${NC}\n"
return 1 return 1
fi fi
@@ -154,7 +166,7 @@ case "$1" in
[ -f "$LOG_FILE" ] && tail -f "$LOG_FILE" || printf "${RED}Log file not found${NC}\n" [ -f "$LOG_FILE" ] && tail -f "$LOG_FILE" || printf "${RED}Log file not found${NC}\n"
;; ;;
status) status)
if [ -n "$(get_port_pids)" ]; then if is_port_listening; then
printf "${GREEN}App is running${NC}\n" 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 (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 else
@@ -174,4 +186,3 @@ case "$1" in
esac esac