I'm working on a computationally heavy code that - for now - crashes a lot, but I'm still working on it :) When it crashes, I can't close the GUI window; I have to open a shell and kill -9 the process.

It is a Java process and it is easy to find:

nkint@zefiro:~$ ps aux | grep java
nkint   2705 16.6  1.0 460928 43680 ?        Sl   12:23   0:08 /usr/lib/jvm/java-6-sun- -Djava.library.path=something something
nkint   2809  0.0  0.0   4012   776 pts/0    S+   12:24   0:00 grep --color=auto java
nkint@zefiro:~$ kill -9 2705

Now it is easy but quite a mechanical task. So normally i wait for about 7-8 processes to crash, and then kill -9 each of them.

I want to do this in an automatic way. I think that it should be easy to pipe some commands to take the id of the (n-1) results of ps aux | grep java and kill it but I don't have any idea where to start.

Can anyone give me any hints?

5 Answers 5


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.

  • 1
    killall java wasn't working for me for some random reason that I wasn't able to resolve. The -9 parameter took care of that. Thank you.
    – Niko
    May 12, 2014 at 21:14
  • 7
    9 is SIGKILL. Program cannot ignore it, and it basically says: terminate yourself now. Jun 3, 2014 at 23:56
  • IntelliJ users may find their IDE has crashed not to mention Tomcat, and many other apps. This is a brute-force way and not recommended on any system with services or productivity apps relying on java.
    – tresf
    Oct 9, 2019 at 2:43
  • Works like a charm, in my case (pycharm) nothing else would work incl. kill -9 Jan 14, 2020 at 11:26
  • Note: the killall command is part of the psmisc package.
    – Guillaume
    Jun 2, 2023 at 7:10

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.


Here is an alternative approach, based on @Dean's earlier answer.

This uses jps to identify the java process.

kill $(jps | grep <MainClass> | awk '{print $1}')

MainClass is a class in your running java program which contains the main method.


I created this as my Modded minecraft server crashes sometimes and java sticks and wont close got fed up and scripted a sh script that will detect if java is running and attempt to kill them all here is the code if anyone wants it

echo "-----------------------------------"
echo "Checking to see if \e[96mJava\e[0m is \e[32mrunning!\e[0m" 
echo "-----------------------------------"
sleep 3
if pgrep -x "java" > /dev/null
    echo "-----------------------------------"
    echo "\e[96mJava\e[0m is \e[32mrunning:\e[0m Attempting To Kill"
    echo "-----------------------------------"
    sleep 2
    killall SGKILL java
    echo "------------------------------------"
    echo "\e[96mJava\e[0m Detection \e[91mFailed:\e[0m Ending Script"
    echo "------------------------------------"
    sleep 1
sleep 1
if pgrep -x "java" > /dev/null
    echo "-----------------------------------------"
    echo "Could not kill \e[96mJava\e[0m, Are you a \e[31mSudo User?\e[0m"
    echo "-----------------------------------------"
    sleep 2
    echo "------------------------------"
    echo "\e[96mJava\e[0m was killed \e[32mSuccsessfully!\e[0m"
    echo "------------------------------"

#Created By Nocturnal_Toker 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .