如何在终端中结束这个Tomcat进程?

59
使用 ps -ef | grep tomcat 命令,我找到了一个正在运行的tomcat服务器。我尝试使用kill -9 {id}命令,但是返回"没有这样的进程"。我做错了什么?
Admins-MacBook-Pro:test-parent tom.maxwell$ ps -ef | grep tomcat
2043706342 39707 39695   0  3:40PM ttys000    0:00.00 grep tomcat
Admins-MacBook-Pro:test-parent tom.maxwell$ kill -9 39707
-bash: kill: (39707) - No such process

6
那实际上是grep在搜索tomcat。 - Ryan Amos
15个回答

176

不需要知道Tomcat的pid(进程ID)就可以杀掉它。您可以使用以下命令来杀死Tomcat:

不必获取Tomcat的pid即可终止它,可以使用以下命令终止Tomcat:

pkill -9 -f tomcat

1
我有两个Tomcat进程,一个是Tomcat 7,另一个是Tomcat 5。为了杀死它们,我使用了略微不同的变体 pkill -9 -f /tomcat7pathpkill -9 -f /tomcat5path,这样我就可以一次杀死一个。这很容易配置为“别名”,因为命令中没有反引号和单引号...非常好的答案。 - Ram Ghadiyaram
1
请注意,“-f”匹配所有命令行参数,而不仅仅是进程名称,因此即使进程名称为“java”,它仍将匹配。 “pgrep -f tomcat”显示将匹配的内容。 - Curtis Yallop
"pkill -9 -f catalina" 对我也有效。我的Eclipse“java”进程在“ps -ef”中也有“tomcat”,但由于某种原因,“pgrep -f tomcat”无法匹配它。 - Curtis Yallop

22

ps -ef | grep tomcat | awk '{print $2}' | xargs kill -9

https://gist.github.com/nrshrivatsan/1d2ea4fcdcb9d1857076

第一部分

ps -ef | grep tomcat =>获取所有包含tomcat进程的信息

第二部分

获取进程详细信息后,将其传输至脚本的第二部分

awk '{print $2}' | xargs kill -9 => 获取第二列(即进程ID),然后使用-9参数杀死它们

希望对您有所帮助。


20

Tomcat没有运行。您的搜索显示了正在搜索Tomcat的grep进程。当然,当您看到该输出时,grep已不再运行,因此pid不再有效。


2
下次使用 ps -ef | grep [t]omcat。 - Bdloul

16

只需在终端中输入以下命令

ps -ef |grep 'catalina'

复制进程ID的值,然后输入以下命令并粘贴进程ID

 kill -9 processid

1
这是我在Linux Mint 18.3上第一个可行的解决方案,所选答案没有效果。 - JesseBoyd
当我通过Eclipse运行Tomcat并且进程名称为“java”时,这对我有效。我的另一个“java”进程是Eclipse。请参见相关答案,其中包含“org.apache.catalina.startup.Bootstrap”。 - Curtis Yallop

12

正如其他人已经提到的那样,您已经看到了grep进程。如果您想将输出限制为Tomcat本身,则有两个选择:

  • wrap the first searched character in a character class

    ps -ef | grep '[t]omcat'
    

    This searches for tomcat too, but misses the grep [t]omcat entry, because it isn't matched by [t]omcat.

  • use a custom output format with ps

    ps -e -o pid,comm | grep tomcat
    

    This shows only the pid and the name of the process without the process arguments. So, grep is listed as grep and not as grep tomcat.


6
ps -Af | grep "tomcat" | grep -v grep | awk '{print$2}' | xargs kill -9

这是最好的答案。谢谢。 - Pinaki Mukherjee

4
tomcat/bin/catalina.sh 文件中,在注释部分结束后添加以下行:
CATALINA_PID=someFile.txt

接下来,要关闭运行中的Tomcat实例,您可以使用以下命令:

kill -9 `cat someFile.txt`

2
ps -ef

将会列出所有当前运行的进程

| grep tomcat

将输出传递给grep并查找tomcat的实例。由于grep本身是一个进程,因此它会从您的命令中返回。但是,您的输出未显示任何正在运行的Tomcat进程。


1
这对我很有帮助:
步骤1:echo ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }' 以上命令返回“进程ID”
步骤2:kill -9 进程ID // 步骤1的输出与此处的进程ID相同

1
为了根据进程名称杀死进程,我使用以下命令:
ps aux | grep "search-term" | grep -v grep | tr -s " " | cut -d " " -f 2 | xargs kill -9
tr -s " " | cut -d " " -f 2 等同于 awk '{print $2}'tr 将制表符转换为单个空格,而 cut 使用 <SPACE> 作为分隔符,并请求第二列。在 ps aux 输出中,第二列是进程 ID。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接