17

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 the Bash version:

RESULT=`netstat -na | grep $2 | awk '{print $7}' | wc -l`

Where $2 is the port.

I am looking for something similar to that. Using Cygwin is out of the question, of necessity this script must be able to run on machines that only have Tomcat.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MirroredFate
  • 12,396
  • 14
  • 68
  • 100
  • Have a look at http://stackoverflow.com/questions/4116957/tomcat-shut-down-through-process-id-windows/4117061#4117061 - it might be slightly irrelevant, but you could possibly see how catalina.bat does it – mindas Jun 02 '11 at 22:39
  • http://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script – expelledboy Jun 16 '11 at 13:48
  • First make sure that path to Tomcat is setup correctly, by running any command like: **C:\>javap javax.servlet.http.HttpServletRequest**. If you can see any result, then Tomcat is setup well. Go ahead and check whether Tomcat is running or not. – shasi kanth Feb 13 '14 at 06:47

10 Answers10

6

Test the status of the Tomcat Service with the SC command. MJB already suggested to test the service status with SC, yet another batch script (without FOR loop) for testing the status:

@ECHO OFF
SC query tomcat5 | FIND "STATE" | FIND "RUNNING" > NUL
IF ERRORLEVEL 1 (
    ECHO Stopped
) ELSE (
    ECHO Running
)

If you are not sure if the service name is tomcat5 you can list all service names with

SC query state= all | FIND "SERVICE_NAME"
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
5

You could use tasklist to check if the tomcat executable is running. For example:

@echo off
tasklist /FI "IMAGENAME eq tomcat.exe" | find /C /I ".exe" > NUL
if %errorlevel%==0 goto :running

echo tomcat is not running
goto :eof

:running
echo tomcat is running

:eof

It is also possible to check a remove server using the options /S, /U and /P. See tasklist /? for details.

wimh
  • 15,072
  • 6
  • 47
  • 98
3

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
grunci
  • 89
  • 1
  • 6
3

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.

RealHowTo
  • 34,977
  • 11
  • 70
  • 85
1

use netstat -a in command prompt.

You'll find 8080 port listed there.

R11G
  • 1,941
  • 8
  • 26
  • 37
1

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.
Anton Lem
  • 91
  • 3
1

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

Cratylus
  • 52,998
  • 69
  • 209
  • 339
0

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

orak
  • 2,399
  • 7
  • 29
  • 55
0

Yet another option, since this is probably running as a service

FOR /F "tokens=4 delims= " %%A IN ('SC QUERY tomcat5 ^| FIND "STATE"') DO SET status=%%A
echo "%status%"

status can be things like STOPPED, RUNNING ...

MJB
  • 9,352
  • 6
  • 34
  • 49
-4

You can try searching for the process and extracting the line

For example:

ps|grep tomcat
Kaisean
  • 23
  • 1
  • 4