MirroredFate
MirroredFate

Reputation: 12816

Determine if Tomcat is running in Windows using the command prompt

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.

Upvotes: 17

Views: 71314

Answers (10)

Anton Lem
Anton Lem

Reputation: 91

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.

Upvotes: 1

orak
orak

Reputation: 2419

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

Upvotes: 0

R11G
R11G

Reputation: 1980

use netstat -a in command prompt.

You'll find 8080 port listed there.

Upvotes: 1

grunci
grunci

Reputation: 89

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

Upvotes: 3

Christian Ammer
Christian Ammer

Reputation: 7542

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"

Upvotes: 6

Kaisean
Kaisean

Reputation: 23

You can try searching for the process and extracting the line

For example:

ps|grep tomcat

Upvotes: -4

RealHowTo
RealHowTo

Reputation: 35372

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.

Upvotes: 3

MJB
MJB

Reputation: 9399

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

Upvotes: 0

wimh
wimh

Reputation: 15232

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.

Upvotes: 5

Cratylus
Cratylus

Reputation: 54074

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

Upvotes: 1

Related Questions