Determine if Tomcat is running in Windows using the command prompt

后端 未结 10 1702
陌清茗
陌清茗 2020-12-15 06:24

Quite simply, how does one determine whether or not Tomcat is running in Windows, using the command prompt?

I am writing a batch script that must do this. This is

相关标签:
10条回答
  • 2020-12-15 06:55

    If you run Tomcat for Windows not like a service and don't want to exploit JMX the best way is

    for /F %%I in ('tasklist /FI "WINDOWTITLE eq Tomcat" /NH') do if %%I==java.exe goto alreadyRun
    

    where:

    • Tomcat - the window title of the Tomcat's terminal window by default
    • java.exe - the name of the Tomcat's processe. NOT tomcat.exe.
    0 讨论(0)
  • 2020-12-15 06:57

    Using WMIC

    @echo off
    wmic process list brief | find /i "tomcat.exe"
    set result=%ERRORLEVEL%
    if "%result%"=="1" echo "not running"
    if "%result%"=="0" echo "running"
    

    note : /i is to make the find operation case-insensitive.

    0 讨论(0)
  • 2020-12-15 06:59

    This is the Windows version of the netstat based UNIX/LINUX solution asked in the question:

    @echo off
    
    netstat -na | find "LISTENING" | find /C /I ":8080" > NUL
    if %errorlevel%==0 goto :running
    
    echo tomcat is not running
    goto :eof
    
    :running
    echo tomcat is running
    
    :eof
    
    0 讨论(0)
  • 2020-12-15 07:00

    I check it by calling a vb script from command line

    cscript //nologo checkurl.vbs | findstr "200"
    IF errorlevel 1 GOTO :not_running 
    

    Save the below script as checkurl.vbs and replace the ip with machines ip

    ' Create an HTTP object
    myURL = "http://10.1.1.1:8080/"
    Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
    
    ' Download the specified URL
    objHTTP.Open "GET", myURL, False
    On Error Resume Next
    objHTTP.Send
    intStatus = objHTTP.Status
    
    If intStatus = 200 Then
        WScript.Echo intStatus
    Else
      WScript.Echo "Error Connecting"
    End If
    

    I had problems with using sc query command, because even if tomcat crashed, the service would still be shown as running where in actual the port was not accessible

    0 讨论(0)
  • 2020-12-15 07:01

    You can try searching for the process and extracting the line

    For example:

    ps|grep tomcat
    
    0 讨论(0)
  • 2020-12-15 07:06

    Well, I am not very good with scripts but perhaps you could use this as a starting point:

    netstat -a -n | findstr :8005

    To get if someone is listening in port 8005. That is Tomcat's default port for remote administration, i.e. startup or shutdown.
    Alternatively you could use the port that the http server listens to.
    Hope this helps

    0 讨论(0)
提交回复
热议问题