Kill only processes (instances) of a particular Java jar

I need to create an automatic script that kills the running processes of certain Java JARs.

I do it manually as follows:

jps -v

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

I pick up specific processes called JARs, like myJarToKill.jar, and run them to kill them.

kill 4573 4576  

Is it possible to get the numbers of these processes using grep or sth like this? Skip it to kill the team?

+4
source share
2 answers

The command used is a combination of grep, awk, and 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>

Before doing this, read the following explanation:

first of all run this: jps -v | grep "myJarToKill.jar" | awk '{print $ 1}'

: $2 , ps , . , , , awk '{print $2}' - , $2 , .

"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 

: kill -TERM, .

Claudio

+1
kill `jps -v |grep myJarToKill|cut -f1 -d " "`

cut -f1 -d " " - , "". `get , kill.

0

Source: https://habr.com/ru/post/1625459/


All Articles