gpt4 book ai didi

windows - 如何找出Tomcat是否已经停止运行批处理

转载 作者:行者123 更新时间:2023-11-28 22:19:35 24 4
gpt4 key购买 nike

我在寻找这个问题的答案,但找不到。

这是我的代码 restart.bat。在我重新启动之前,我需要检查 Tomcat 是否已停止。现在脚本不等待 Tomcat。

@echo off
set "CATALINA_HOME=C:\DIR\Tomcat"
set "STOP=%CATALINA_HOME%\bin\shutdown.bat"
set "START=%CATALINA_HOME%\bin\startup.bat"
@echo on
call %STOP%
TIMEOUT /T 2

Rem trying to wait Tomcat Stop
Rem How to know it?

call %START%
TIMEOUT /T 2

当我们可以在 catalina.YYYY-MM-DD.log 文件中读取以下行(消息)时,Tomcat 正在运行并能够响应请求:

mmm dd, YYYY HH:mm: ss xM org.apache.catalina.startup.Catalina start
INFO: Server startup in 106737 ms

当我们可以在 catalina.YYYY-MM-DD.log 文件中读取以下行(消息)时,Tomcat 肯定已停止:

mmm dd, YYYY HH:mm: ss xM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-bio-8080"]
mmm dd, YYYY HH:mm: ss xM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-bio-8009"]

然后我想到了另一个片段running.bat脚本:

@echo off
tasklist /FI "SessionName eq services" | find /I "tomcat" | find /I ".exe"> NUL
if %errorlevel%==0 goto :run
echo Tomcat is not running
goto :eof
:run
echo Tomcat is running
:eof

如何只创建一个脚本?

最佳答案

这应该可以满足您的需求。正如您所建议的,您不需要检查日志,因为任务列表中的 tomcat 状态可以告诉您它是否正在运行。我插入了“等待”代码,因此每秒检查一次任务列表,直到 Tomcat 停止。

@echo off
set "CATALINA_HOME=C:\DIR\Tomcat"
set "STOP=%CATALINA_HOME%\bin\shutdown.bat"
set "START=%CATALINA_HOME%\bin\startup.bat"

:stop
call %STOP%
call :runstat
echo Tomcat has stopped
pause
call %START%

:runstat
tasklist /FI "SessionName eq services" | find /I "tomcat" | find /I ".exe"> NUL
set err=%errorlevel%

:: see if stopped condition is met and if yes exit
if %err%==1 exit /b
:: else wait 1 second and try again
PING 127.0.0.1 -n 60 >NUL 2>&1 || PING ::1 -n 60 >NUL 2>&1
goto runstat

工作原理:label :runstat 中的代码查看任务列表错误级别以确定 Tomcat 是否正在运行,并在 Tomcat 停止时返回调用例程——返回错误级别 1。在返回 1 之前,ping 会在通过 goto 再次检查任务列表之前插入一秒钟的延迟。

(多年来我一直使用这种很棒的 ping 方法来插入延迟。时间 (60) 以毫秒为单位,可以调整。这里有解释:http://www.robvanderwoude.com/wait.php)

关于windows - 如何找出Tomcat是否已经停止运行批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26941864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com