How to quickly kill Java processes in bash?

In a linux window, no more than 3 java jars files work for me. How to quickly kill all 3 with one command?

Normally I would:

ps ex - get running processes

then find the process ids:

kill -9 #### #### ####

Any way to shorten this process? My eyes ache with a pinch to find process identifiers.

My script does the following:

nohup ./start-gossip & nohup ./start & nohup ./start-admin & 

Is there a way to get process identifiers for each of them without looking at it?

+4
source share
3 answers

You can save the PID when starting processes, so you can use them later:

 nohup ./start-gossip & START_GOSSIP_PID=$! nohup ./start & START_PID=$! nohup ./start-admin & START_ADMIN_PID=$! ... kill -9 $START_GOSSIP_PID kill -9 $START_PID kill -9 $START_ADMIN_PID 

This has the advantage (more than pkill ) of not destroying any other processes that coincidentally have similar names. If you do not want to perform the kill operation from the script itself, but just want the PIDs to be convenient, write them to a file (from the script):

 echo $START_GOSSIP_PID > /some/path/start_gossip.pid 

Or just do it when you start the process, and not save the PID for the variable:

 nohup ./start-gossip & echo $! > /some/path/start_gossip.pid 
+1
source

Short answer:

 pkill java 

It searches for the process (or processes) to kill by name. It will also find other java processes, so be careful. It also accepts -9 , but you should avoid using -9 unless something is really broken.

EDIT:

Based on the updates, you can specify the script names in pkill (I'm not sure). But, a more traditional way to deal with this problem is to leave pid files. After starting a new process and creating it, its pid is available in $! . If you write the pid file to a file, then it is easy to check if the process is working and kill only the processes that you mean. However, it is likely that pid will be reused.

+13
source

To get the process id in this java process

netstat -tuplen

The process identifier (PID) of this process that you want to kill and run

kill -9 PID

-1
source

All Articles