start_server.bat 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. @echo off
  2. chcp 65001 >nul
  3. setlocal EnableDelayedExpansion
  4. rem Set basic variables
  5. set "SCRIPT_DIR=%~dp0"
  6. set "LOG_DIR=%SCRIPT_DIR%logs"
  7. set "BACKEND_PID_FILE=%SCRIPT_DIR%backend.pid"
  8. if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"
  9. set "LOG_FILE=%LOG_DIR%\server_%date:~0,4%%date:~5,2%%date:~8,2%.log"
  10. rem Clean up old processes and files
  11. taskkill /F /IM python.exe /FI "WINDOWTITLE eq OpenWebUI*" >nul 2>&1
  12. timeout /t 2 >nul
  13. if exist "%BACKEND_PID_FILE%" del "%BACKEND_PID_FILE%"
  14. goto :main
  15. :write_log
  16. echo [%date% %time%] %~1
  17. echo [%date% %time%] %~1 >> "%LOG_FILE%"
  18. exit /b
  19. :start_backend
  20. cd /d "%SCRIPT_DIR%backend"
  21. call :write_log "Starting backend service..."
  22. start "OpenWebUI Backend" /B cmd /c start_windows.bat
  23. if errorlevel 1 (
  24. call :write_log "Failed to start backend"
  25. exit /b 1
  26. )
  27. timeout /t 5 >nul
  28. for /f "tokens=2" %%a in ('wmic process where "commandline like '%%python%%' and name='python.exe'" get processid ^| findstr /r "[0-9]"') do (
  29. echo %%a > "%BACKEND_PID_FILE%"
  30. call :write_log "Backend started with PID: %%a"
  31. exit /b 0
  32. )
  33. call :write_log "Failed to get backend PID"
  34. exit /b 1
  35. :start
  36. call :write_log "Starting backend service..."
  37. call conda activate open-webui
  38. if errorlevel 1 (
  39. call :write_log "Failed to activate conda environment"
  40. goto :cleanup
  41. )
  42. call :start_backend
  43. if errorlevel 1 goto :cleanup
  44. cd /d "%SCRIPT_DIR%"
  45. call :write_log "Backend service started successfully"
  46. exit /b 0
  47. :cleanup
  48. call :write_log "Start failed, cleaning up..."
  49. call :stop
  50. exit /b 1
  51. :stop
  52. call :write_log "Stopping services..."
  53. if exist "%BACKEND_PID_FILE%" (
  54. for /f %%a in (%BACKEND_PID_FILE%) do (
  55. taskkill /F /T /PID %%a >nul 2>&1
  56. if not errorlevel 1 call :write_log "Stopped backend process (PID: %%a)"
  57. )
  58. del "%BACKEND_PID_FILE%"
  59. )
  60. for /f "tokens=2" %%a in ('wmic process where "commandline like '%%python%%' and name='python.exe'" get processid ^| findstr /r "[0-9]"') do (
  61. taskkill /F /T /PID %%a >nul 2>&1
  62. call :write_log "Stopped additional Python process (PID: %%a)"
  63. )
  64. call :write_log "All services stopped"
  65. exit /b 0
  66. :status
  67. set "running=0"
  68. if exist "%BACKEND_PID_FILE%" (
  69. for /f %%a in (%BACKEND_PID_FILE%) do (
  70. tasklist /FI "PID eq %%a" 2>nul | find "%%a" >nul
  71. if not errorlevel 1 (
  72. call :write_log "Backend is running (PID: %%a)"
  73. set /a running+=1
  74. )
  75. )
  76. )
  77. if %running% equ 1 (
  78. call :write_log "Backend service is running"
  79. exit /b 0
  80. ) else (
  81. call :write_log "Backend service is not running"
  82. exit /b 1
  83. )
  84. :main
  85. if "%1"=="" (
  86. echo Usage: %~nx0 {start^|stop^|status}
  87. exit /b 1
  88. )
  89. if "%1"=="start" (
  90. call :stop
  91. timeout /t 2 >nul
  92. call :start
  93. ) else if "%1"=="stop" (
  94. call :stop
  95. ) else if "%1"=="status" (
  96. call :status
  97. ) else (
  98. echo Invalid command. Use: %~nx0 {start^|stop^|status}
  99. exit /b 1
  100. )
  101. exit /b %errorlevel%