Java 检查tomcat是否运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4095840/
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
check the tomcat running or not?
提问by krishna
Possible Duplicate:
Is Tomcat running?
可能的重复:
Tomcat 是否正在运行?
hi,
你好,
i installed tomcat server in virtual machine. i want to check the tomcat server daily, it is running or not. if it is running or not it will send mail to my mail id. how i achieve?
我在虚拟机中安装了 tomcat 服务器。我想每天检查 tomcat 服务器,它是否正在运行。如果它正在运行,它将向我的邮件 ID 发送邮件。我如何实现?
can i do this one by using batch files
我可以使用批处理文件来做这个吗
Thanks Murali
谢谢穆拉利
回答by Prabhakaran
if your tomcat is in linux environment then you can use netstat command to check for the tomcat listening port 8080 and send mail using shell script
如果你的tomcat是在linux环境下,那么你可以使用netstat命令来检查tomcat监听端口8080并使用shell脚本发送邮件
回答by dogbane
You can cron the following script which checks the status of the tomcat pid present in the $CATALINA_PID file. If the pid is dead, an email is sent.
您可以 cron 以下脚本检查 $CATALINA_PID 文件中存在的 tomcat pid 的状态。如果 pid 已死,则会发送一封电子邮件。
kill -0 `cat $CATALINA_PID` > /dev/null 2>&1
if [ $? -gt 0 ]
then
echo "Check tomcat" | mailx -s "Tomcat not running" [email protected]
fi
回答by Buhake Sindi
If tomcat is running, the easiest way (platform independent) is to type this url to any web browser (assuming your http port is configured to "80"): http://localhost:8080/. You can change localhost to any host server of choice. This will open a Tomcat page with the following message:
如果 tomcat 正在运行,最简单的方法(与平台无关)是在任何 Web 浏览器中输入此 url(假设您的 http 端口配置为“80”):http://localhost:8080/。您可以将 localhost 更改为任何选择的主机服务器。这将打开一个带有以下消息的 Tomcat 页面:
If you're seeing this page via a web browser, it means you've setup Tomcat successfully. Congratulations!
如果您通过 Web 浏览器看到此页面,则表示您已成功设置 Tomcat。恭喜!
Otherwise netstat and see if anything runs on port 8080.
否则 netstat 并查看端口 8080 上是否有任何运行。
PSI just read that you want to send email notification if not running, the best way is to create a job that periodically 1) listens to port 8080 or 2) find if a tomcat process is up and running. Failure to meet 1) or 2) sends an email to system administrator.
PS我刚刚读到如果没有运行你想发送电子邮件通知,最好的方法是创建一个定期 1) 侦听端口 8080 或 2) 查找 tomcat 进程是否启动并运行的作业。未能满足 1) 或 2) 向系统管理员发送电子邮件。
回答by Stephen C
There are multiple answers to this, depending on what you mean by "running". For instance, @dogbane's answer tells you if a Tomcat process exists (modulo edge cases), and @Prabhakaran's answer tells you if Tomcat is listening on port 8080 (modulo different edge cases). But neither of these will pick up cases where Tomcat has stopped responding to requests.
对此有多种答案,具体取决于您所说的“跑步”是什么意思。例如,@dogbane 的回答会告诉您 Tomcat 进程是否存在(模边界情况),@Prabhakaran 的答案会告诉您 Tomcat 是否正在侦听端口 8080(模不同的边界情况)。但是这些都不会处理 Tomcat 停止响应请求的情况。
@Elite's answer tells you if the default (e.g. ROOT) servlet is running, but this is probably not enough.
@Elite 的回答会告诉您默认(例如 ROOT)servlet 是否正在运行,但这可能还不够。
I'd recommend the following approach:
我推荐以下方法:
- Pick some webapp pages that you care about. Ideally these should include pages that depend on back-end services / databases.
- Create request URLs to fetch the pages, perform the queries, or whatever/
- Use
curl
orwget
to send the request URLs. - Use some kind of pattern matching to make sure that you are getting good results and not error pages.
- If you get timeouts, unexpected response codes or error pages, crank out a mail message1.
- 选择一些您关心的 webapp 页面。理想情况下,这些应该包括依赖于后端服务/数据库的页面。
- 创建请求 URL 以获取页面、执行查询或其他任何/
- 使用
curl
或wget
发送请求 URL。 - 使用某种模式匹配来确保您获得良好的结果而不是错误页面。
- 如果出现超时、意外响应代码或错误页面,请发送邮件消息1。
This kind of thing is easier to do if you are running on a UNIX / Linux platform than on Windows because the tools you need to implement it should be either installed already, or easy to install using the package manager.
如果您在 UNIX/Linux 平台上运行,这种事情比在 Windows 上运行更容易,因为您需要实现它的工具应该已经安装,或者可以使用包管理器轻松安装。
1 - Actually, that's a recipe for spamming yourself. A better idea is to deploy an event monitoring system that understands system states, does duplicate suppression and so on.
1 - 实际上,这是给自己发送垃圾邮件的秘诀。更好的想法是部署一个了解系统状态、执行重复抑制等的事件监控系统。