使用命令提示符确定 Tomcat 是否在 Windows 中运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6220196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if Tomcat is running in Windows using the command prompt
提问by MirroredFate
Quite simply, how does one determine whether or not Tomcat is running in Windows, using the command prompt?
很简单,如何使用命令提示符确定 Tomcat 是否在 Windows 中运行?
I am writing a batch script that must do this. This is the Bashversion:
我正在编写一个必须执行此操作的批处理脚本。这是Bash版本:
RESULT=`netstat -na | grep | awk '{print }' | wc -l`
Where $2 is the port.
其中 $2 是端口。
I am looking for something similar to that. Using Cygwinis out of the question, of necessity this script must be able to run on machines that only have Tomcat.
我正在寻找类似的东西。使用Cygwin是不可能的,这个脚本必须能够在只有 Tomcat 的机器上运行。
采纳答案by wimh
You could use tasklist to check if the tomcat executable is running. For example:
您可以使用 tasklist 检查 tomcat 可执行文件是否正在运行。例如:
@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.
也可以使用选项检查删除服务器/S
,/U
和/P
。详情请参阅tasklist /?
。
回答by Christian Ammer
Test the status of the Tomcat Service with the SCcommand. MJB already suggested to test the service status with SC, yet another batch script (without FOR loop) for testing the status:
使用SC命令测试 Tomcat 服务的状态。MJB 已经建议用 SC 测试服务状态,还有另一个用于测试状态的批处理脚本(没有 FOR 循环):
@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 tomcat5you can list all service names with
如果您不确定服务名称是否为tomcat5,您可以列出所有服务名称
SC query state= all | FIND "SERVICE_NAME"
回答by grunci
This is the Windows version of the netstat based UNIX/LINUX solution asked in the question:
这是问题中提出的基于 netstat 的 UNIX/LINUX 解决方案的 Windows 版本:
@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
回答by RealHowTo
回答by Anton Lem
If you run Tomcat for Windows not like a serviceand don't want to exploit JMX the best way is
如果您运行 Tomcat for Windows不像服务那样并且不想利用 JMX,那么最好的方法是
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.
- Tomcat - 默认情况下 Tomcat 终端窗口的窗口标题
- java.exe - Tomcat 进程的名称。不是 tomcat.exe。
回答by R11G
use netstat -a in command prompt.
在命令提示符下使用 netstat -a。
You'll find 8080 port listed there.
您会在那里找到 8080 端口。
回答by Cratylus
Well, I am not very good with scripts but perhaps you could use this as a starting point:
好吧,我不太擅长脚本,但也许您可以将其用作起点:
netstat -a -n | findstr :8005
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
获取是否有人正在侦听端口 8005。这是 Tomcat 用于远程管理的默认端口,即启动或关闭。
或者,您可以使用 http 服务器侦听的端口。
希望这可以帮助
回答by MJB
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 ...
状态可以是诸如 STOPPED、RUNNING ...
回答by orak
I check it by calling a vb script from command line
我通过从命令行调用 vb 脚本来检查它
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
将下面的脚本保存为 checkurl.vbs 并用机器 ip 替换 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
我在使用sc query
命令时遇到问题,因为即使 tomcat 崩溃了,该服务仍会显示为正在运行,而实际上端口无法访问
回答by Kaisean
You can try searching for the process and extracting the line
您可以尝试搜索进程并提取行
For example:
例如:
ps|grep tomcat