Files
DanDing1Todo_Web/run.sh
Mr.Xia d57b45522c
Some checks failed
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled
修1
2026-02-27 11:02:05 +08:00

134 lines
3.6 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# --- 颜色定义 ---
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
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
}
# --- 逻è¾å°<C3A5>装 ---
function do_stop() {
printf "${YELLOW}正在å<EFBFBD>œæ­¢ STARK Todo List...${NC}\n"
# 1. é¦å…ˆå°<C3A5>试通过 PID æ‡ä»¶å<C2B6>œæ­¢
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
pkill -P $PID >/dev/null 2>&1
kill $PID >/dev/null 2>&1
rm "$PID_FILE"
fi
# 2. 强åŠå…œåº•:查找并æ<C2B6>€æ­»å<C2BB> ç”?2333 端å<C2AF>£çš„æ‰€æœ?Node 进程
PORT_PIDS=$(get_port_pids)
if [ -n "$PORT_PIDS" ]; then
for P in $PORT_PIDS; do
kill -9 $P >/dev/null 2>&1
done
fi
printf "${YELLOW}å·²å<EFBFBD>œæ­¢æ‰€æœ‰ç¸å…³è¿ç¨å¹¶é‡Šæ”¾ç«¯å<EFBFBD>£ã€?{NC}\n"
}
function do_start() {
if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
printf "${YELLOW}应用已ç»<EFBFBD>在è¿<EFBFBD>行中 (PID: $(cat $PID_FILE))${NC}\n"
return 0
fi
PORT_PID=$(get_port_pids)
if [ -n "$PORT_PID" ]; then
printf "${YELLOW}?? 2333 ??????????????...${NC}\n"
do_stop
sleep 1
PORT_PID=$(get_port_pids)
if [ -n "$PORT_PID" ]; then
printf "${RED}??: ?? 2333 ?????????????????${NC}\n"
return 1
fi
fi
printf "${GREEN}?? $PM ?? STARK Todo List (production)...${NC}\n"
# æŠåˆ¶å®‰è£…输出,ä¿<C3A4>æŒ<C3A6>界é<C592>¢æ•´æ´? $PM install > /dev/null 2>&1
$PM run build > /dev/null 2>&1
NODE_ENV=production nohup $PM run start > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
sleep 2
if kill -0 $(cat "$PID_FILE") 2>/dev/null; then
printf "${GREEN}å<EFBFBD>¯åЍæˆ<EFBFBD>功ï¼<EFBFBD>访é—? http://localhost:2333${NC}\n"
else
printf "${RED}å<EFBFBD>¯åŠ¨å¤±è´¥ï¼Œè¯·æŸ¥çœ $LOG_FILE${NC}\n"
rm -f "$PID_FILE"
fi
}
# --- åˆ<C3A5>å§åŒ?---
if [ ! -f "todos.json" ]; then
echo "[]" > todos.json
printf "${YELLOW}å·²åˆå»ºåˆ<EFBFBD>å§æ•°æ<EFBFBD>®æ‡ä»?todos.json${NC}\n"
fi
# 检查包管ç<C2A1>†å™?
if command -v pnpm >/dev/null 2>&1; then
PM="pnpm"
else
PM="npm"
fi
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
printf "${BLUE}正在执行彻底é‡<EFBFBD>å<EFBFBD>¯...${NC}\n"
do_stop
sleep 1
rm -rf .next
do_start
;;
logs)
[ -f "$LOG_FILE" ] && tail -f "$LOG_FILE" || printf "${RED}日志æ‡ä»¶ä¸<EFBFBD>å­˜åœ?{NC}\n"
;;
status)
if [ -n "$(get_port_pids)" ]; then
printf "${GREEN}应用正在è¿<EFBFBD>行${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
printf "${RED}应用未è¿<EFBFBD>è¡?{NC}\n"
fi
;;
clean)
printf "${BLUE}清ç<EFBFBD>†ç¼“å­˜åŒæ—¥å¿?..${NC}\n"
rm -rf .next "$LOG_FILE" "$PID_FILE"
printf "${GREEN}清ç<EFBFBD>†å®Œæˆ<EFBFBD>${NC}\n"
;;
*)
printf "${BLUE}STARK Todo List 本地管ç<C2A1>†è„šæœ¬${NC}\n"
echo "用法: $0 [start|stop|restart|logs|status|clean]"
exit 1
;;
esac