Kill Java processes

If you want to kill all processes that are named java you can use following command:

killall -9 java

This command sends signals to processes identified by their name.


A few more pipes will get you where you want to be. Here's how I'd do it:

search_terms='whatever will help find the specific process' 

kill $(ps aux | grep "$search_terms" | grep -v 'grep' | awk '{print $2}')

Here's what's happening:

grep -v 'grep' excludes the grep process from the the results.

awk '{print $2}' prints only the 2nd column of the output (in this case the PID)

$(...) is command substitution. Basically the result from the inner command will be used as an argument to kill

This has the benefit of finer control over what is killed. For example, if you're on a shared system you can edit the search terms so that it only attempts to kill your own java processes.


Open a text editor and save this short bash script in your home directory as 'killjava'

#! /bin/bash

# Kill Java process

# Determine the pid
PID=`ps -C java -o pid=`

kill -9 $PID

Then chmod u+x ~/killjava in a terminal so you can execute the file.

Then you can just call ~/killjava from a terminal and your Java process will be stone dead. You may wish to consider what other resources your killing of the Java process in this way (such as database connections) will be affected. For example, perhaps kill -15 would be more appropriate - see the explanation here.

Tags:

Process

Pipe

Kill