Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly kill java processes in bash?

Tags:

bash

kill

On a linux box, I have at most 3 java jars files running. How do I quickly kill all 3 with one command?

Usually I would:

ps ex - get the processes running

then find the process ids then do:

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

Any way to shorten this process? My eyes hurts from squinting to find the process ids.

My script does the following:

nohup ./start-gossip &

nohup ./start &

nohup ./start-admin &

Is there a way to get the process ids of each without looking it up?

like image 219
iCodeLikeImDrunk Avatar asked Mar 19 '13 19:03

iCodeLikeImDrunk


People also ask

How do you kill a java process?

Use jps to list running java processes. The command returns the process id along with the main class. You can use kill command to kill the process with the returned id or use following one liner script. MainClass is a class in your running java program which contains the main method.

What is kill 9 in Linux?

“ kill -9” command sends a kill signal to terminate any process immediately when attached with a PID or a processname. It is a forceful way to kill/terminate a or set of processes. “ kill -9 <pid> / <processname>” sends SIGKILL (9) — Kill signal. This signal cannot be handled (caught), ignored or blocked.

How do I kill a java process in Task Manager?

If you want to kill all java.exe processes : taskkill /F /IM java.exe /T .

How do you kill a process in Shell?

There are two commands used to kill a process: kill – Kill a process by ID. killall – Kill a process by name.


2 Answers

Short answer:

pkill java

This looks up a process (or processes) to kill by name. This will find any other java processes too, so be careful. It also accepts -9, but you should avoid using -9 unless something is really broken.

EDIT:

Based on updates, you may be able to specify the script names to pkill as well (I'm not positive). But, the more traditional way to handle this issue is to leave pid files around. After you start a new process and background it, its pid is available in $!. If you write that pid to a file, then it's easy to check if the process is still running and kill just the processes you mean to. There is some chance that the pid will be reused, however.

like image 164
FatalError Avatar answered Sep 26 '22 22:09

FatalError


You can save the PIDs when you start the 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 (over pkill) of not killing off any other processes that coincidentally have similar names. If you don't want to perform the kill operation from the script itself, but just want to have the PIDs handy, write them to a file (from the script):

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

Or even just do this when you launch the process, rather than saving the PID to a variable:

nohup ./start-gossip &
echo $! > /some/path/start_gossip.pid
like image 40
davmac Avatar answered Sep 23 '22 22:09

davmac