Files
pricing_review/AdminCost/启动.bat

114 lines
2.6 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters
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.
@echo off
cd /d "%~dp0"
title 标签管理服务(关闭本窗口即停止)
set "PORT=8765"
set "FORCE="
if not "%~1"=="" set "PORT=%~1"
if /i "%~2"=="--restart" set "FORCE=1"
rem ---------- 找 Python ----------
set "PY="
where python >nul 2>nul
if not errorlevel 1 set "PY=python"
if not defined PY (
where py >nul 2>nul
if not errorlevel 1 set "PY=py"
)
if not defined PY (
echo.
echo [错误] 没有找到 Python 3。
echo 请先安装 Python,并在安装时勾选 Add Python to PATH。
echo.
pause
exit /b 1
)
rem ---------- 本服务是不是已经在跑 ----------
call :probe
if not "%ALIVE%"=="1" goto :start
if defined FORCE goto :restart
echo.
echo 服务已经在运行:http://127.0.0.1:%PORT%
echo.
echo 按 Y 再回车 -^> 重启服务(结束旧进程,改用最新代码)
echo 按 N 再回车 -^> 只打开网页,不动正在跑的服务
echo.
echo (15 秒不选 = N,只打开网页)
echo.
choice /c YN /n /t 15 /d N
if errorlevel 2 goto :openonly
:restart
echo.
echo 正在结束旧进程……
title 正在重启服务
taskkill /FI "WINDOWTITLE eq 标签管理服务*" /T /F >nul 2>nul
title 标签管理服务(关闭本窗口即停止)
%PY% app.py %PORT% --stop
if errorlevel 1 (
echo.
echo [错误] 旧进程没能结束(端口 %PORT% 仍被占用)。
echo 请手动关掉标题为「标签管理服务」的黑窗口,再双击本文件。
echo.
pause
exit /b 1
)
echo 好了,正在用最新代码重新启动。
echo.
:start
rem ---------- 端口被别的程序占着 ----------
call :isListening
if "%LISTEN%"=="1" (
echo.
echo [错误] 端口 %PORT% 被别的程序占用了(不是本服务)。
echo 换个端口启动,例如:启动.bat 8766
echo.
pause
exit /b 1
)
echo 目录 :%CD%
echo Python :%PY%
echo 端口 :%PORT%
echo.
echo 正在启动服务…… 端口就绪后会自动打开浏览器。
echo 如果浏览器没弹出来,手动把这个地址复制到浏览器:http://127.0.0.1:%PORT%
echo 关闭本窗口(或按 Ctrl+C)即停止服务。
echo ------------------------------------------------------------
echo.
%PY% app.py %PORT% --open
set "CODE=%ERRORLEVEL%"
echo.
echo ------------------------------------------------------------
echo 服务已退出(退出码 %CODE%)
if not "%CODE%"=="0" echo 上面若有报错,把这整段文字发给我。
echo.
pause
exit /b %CODE%
:openonly
echo.
echo 已打开网页(服务未重启)。要加载最新代码,重新双击本文件后按 Y。
start "" "http://127.0.0.1:%PORT%"
ping -n 3 127.0.0.1 >nul
exit /b 0
rem ---------- 子过程:本服务是否活着 ----------
:probe
set "ALIVE=0"
%PY% -c "import urllib.request;urllib.request.urlopen('http://127.0.0.1:%PORT%/api/state',timeout=2)" >nul 2>nul
if not errorlevel 1 set "ALIVE=1"
exit /b 0
rem ---------- 子过程:端口是否被监听 ----------
:isListening
set "LISTEN=0"
netstat -ano | findstr "LISTENING" | findstr ":%PORT% " >nul
if not errorlevel 1 set "LISTEN=1"
exit /b 0