运维开发网

在Linux中仅杀死特定Java jar的进程(实例)

运维开发网 https://www.qedev.com 2020-06-15 12:43 出处:网络
我需要创建自动脚本,它会杀死特定 Java JAR的运行进程. 我像这样手动完成:
我需要创建自动脚本,它会杀死特定 Java JAR的运行进程.

我像这样手动完成:

jps -v

6753 Jps
4573 myJarToKill.jar
4574 notMyJarToKill.jar
4576 myJarToKill.jar

我根据JAR名称选择特定进程,例如myJarToKill.jar并运行以杀死它们.

kill 4573 4576

是否有可能通过这样的grep或sth获得这些进程的数量?通过它来杀死命令?

要使用的命令是grep,awk和xargs unix命令的组合:

jps -v | grep "<your file name>" | grep -v "<if you need to exclude other output>" |awk '{print $<field number>}'|xargs kill -<kill signal>

在执行之前,请阅读以下说明:

首先运行这个:

    jps -v | grep“myJarToKill.jar”| awk'{print $1}’

Note:

$2 means that the ps output is splitted in space separated field.

So when you run the command for the first time please check that awk '{print $2}' output is the expected result otherwise you should change the field number $2 with the ones that you need.

如果“notMyJarToKill.jar”仍然存在,请添加:

jps -v  | grep "myJarToKill.jar" | grep -v "notMyJarToKill.jar"| awk '{print $1}'

然后,如果输出结果包含您要杀死的pid,则可以运行此命令

jps -v  | grep "myJarToKill.jar" | awk '{print $1}'|xargs kill -9

Note: you could also use kill -TERM it’s depend by your needs.

问候克劳迪奥

0

精彩评论

暂无评论...
验证码 换一张
取 消