如何通过命令行启动停止 Apache Tomcat? 检查 Tomcat 是否已经在运行并 Kill 命令
已发表: 2019-05-06 Apache Tomcat(或简称 Tomcat)是由 Apache Software Foundation (ASF) 开发的开源 Web 服务器和 servlet 容器。 Tomcat 实现了 Oracle Corporation 的 Java Servlet 和 JavaServer Pages ( JSP
) 规范,并为 Java 代码运行提供了一个“纯 Java” HTTP web server
环境。
如果您有以下任何问题,那么您来对地方了:
- Tomcat 服务器在 localhost 所需的几个端口(8080、8081、8082)已在使用中
- Tomcat 服务器错误 – 端口 8080 已在使用中
- 所需端口 8080 正在使用中
- 端口 8080 已经在使用 eclipse
- 如何在Windows中停止端口8080
我已将tomcat as Windows Service
。 将 Tomcat 作为 Windows 服务运行提供了许多在从开发设置转移到生产环境时必不可少的好处。
优点 1) 设置可靠的开机自动启动
- 在您可能希望在维护后远程重新启动 Java 系统而不担心您的服务器是否会重新联机的环境中必不可少。
Benefit-2) 设置 Tomcat 服务器启动,无需主动用户登录
- 在数据中心,期望系统主动登录只是为了运行 Tomcat 是不合理的。 事实上,Tomcat 经常在刀片服务器上运行,这些刀片服务器甚至可能没有连接到它们的活动监视器。 Windows 服务归系统所有,无需活动用户即可启动。
好处-3) 更好的安全性
最近我想通过命令行
start/stop
我的 Tomcat 服务器,因为我想创建快速的脚本来完成它。 官方文档以//XX// ServiceName
的形式提供以下命令
可用的命令行选项有:
-
//TS//
将服务作为控制台应用程序运行 这是默认操作。 如果未提供任何选项,则调用它。 ServiceName是不带exe后缀的可执行文件名,意思是Tomcat6 -
//RS//
运行服务只从ServiceManager调用 //SS//
停止服务//US//
更新服务参数//IS//
安装服务//DS//
删除服务 如果正在运行则停止服务
但是这样做我发现下面的命令非常有用和简单。
1) Windows(如果 Tomcat 设置为 Windows 服务)
-
Start
服务器:<Tomcat Root>/bin>Tomcat8.exe start -
Stop
服务器:<Tomcat Root>/bin>Tomcat8.exe stop
2) Windows(如果您已将二进制文件下载为 .zip)
-
Start
服务器:<Tomcat Root>/bin>catalina.bat start
-
Stop
服务器:<Tomcat Root>/bin>catalina.bat stop
3) Mac/Linux/Unix(如果您已将二进制文件下载为 .zip)
-
Start
服务器:<Tomcat Root>/bin>./catalina.sh start -
Stop
服务器:<Tomcat Root>/bin>./catalina.sh stop
以下是所有catalina.sh
命令参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Usage : catalina . sh ( commands . . . ) commands : debug Start Catalina in a debugger debug - security Debug Catalina with a security manager jpda start Start Catalina under JPDA debugger run Start Catalina in the current window run - security Start in the current window with security manager start Start Catalina in a separate window start - security Start in a separate window with security manager stop Stop Catalina , waiting up to 5 seconds for the process to end stop n Stop Catalina , waiting up to n seconds for the process to end stop - force Stop Catalina , wait up to 5 seconds and then use kill - KILL if still running stop n - force Stop Catalina , wait up to n seconds and then use kill - KILL if still running configtest Run a basic syntax check on server . xml - check exit code for result version What version of tomcat are you running ? |

启动截图:
如何检查 Tomcat 是否已经在运行并终止现有的 tomcat 进程。
Step-1) 使用命令 ps -ef | 找出进程grep tomcat
1 2 3 |
bash - 3.2 $ ps - ef | grep tomcat 502 56188 1 0 7 : 31PM ttys001 0 : 04.23 / Library / Java / JavaVirtualMachines / jdk1 . 8.0_51.jdk / Contents / Home / bin / java - Djava . util . logging . config . file =/ Users / appshah / Downloads / apache - tomcat - 8.5.4 / conf / logging . properties - Djava . util . logging . manager = org . apache . juli . ClassLoaderLogManager - Djdk . tls . ephemeralDHKeySize = 2048 - classpath / Users / appshah / Downloads / apache - tomcat - 8.5.4 / bin / bootstrap . jar : / Users / appshah / Downloads / apache - tomcat - 8.5.4 / bin / tomcat - juli . jar - Dcatalina . base =/ Users / appshah / Downloads / apache - tomcat - 8.5.4 - Dcatalina . home =/ Users / appshah / Downloads / apache - tomcat - 8.5.4 - Djava . io . tmpdir =/ Users / appshah / Downloads / apache - tomcat - 8.5.4 / temp org . apache . catalina . startup . Bootstrap start 502 56618 55587 0 7 : 34PM ttys001 0 : 00.00 grep tomcat |
这里第二列的值是一个进程 ID。 在我们的例子中,它是56188
。
您可以访问链接http://localhost:8080
,您应该会看到欢迎页面。
Step-2) 使用命令 kill -9 <process ID> 杀死进程
1 |
bash - 3.2 $ kill - 9 56188 |
这里, 56188
是我们从步骤 1 中获得的process ID
。
现在,链接http://localhost:8080/
不应该为您工作。