Windows 如何查明Tomcat是否已停止批处理运行

Windows 如何查明Tomcat是否已停止批处理运行,windows,tomcat,batch-file,cmd,restart,Windows,Tomcat,Batch File,Cmd,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% TIM

我想找到这个问题的答案,但没找到

这是我的代码重新启动.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.yyy-MM-DD.log文件中读取以下行(消息)时,Tomcat正在运行并能够响应请求:

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

然后我想到了另一个片段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中的代码查看tasklist errorlevel以确定Tomcat是否正在运行,并在Tomcat停止时退出调用例程——返回errorlevel 1。在返回1之前,ping会在通过goto再次检查任务列表之前插入一秒钟的延迟

(我多年来一直使用这种伟大的ping方法插入延迟。时间(60)以毫秒为单位,可以调整。这里解释如下:)

@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
@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