Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I usually run few Java applications, one for server running locally and other for some IDE like NetBeans. And from time to time, after lots of redeployments, my server get stuck on OutOfMemoryException so I need to kill Java process in order to reboot.

So I do pkill -9 java but this also kills my running IDE which I don't want to. So how do I kill only application linked to running server and not the other ones?I assume that they all are running under same process but there has to be some way how to distuingish them.

share|improve this question
2  
look at your process table (via top or ps) and choose the right one and kill it by PID (kill -9 PID_number). –  rush Feb 13 '13 at 8:25
    
Oh yes, I can see that now. So I just need to kill process with right PID. Thanks a lot –  Petr Mensik Feb 13 '13 at 8:27
add comment

3 Answers 3

up vote 3 down vote accepted

For killing a process that is associated with multiple processes, you need to kill that by using process id associated with that process.

To get the process id of that java process run

ps -A |grep java

output of this command will give the list of java processes running on your system. Note down Process ID (PID) of that process whom you want to kill and run

kill -9 PID
share|improve this answer
add comment

Instead of using ps and grep, you can use ps's -C flag to select all commands listed with the name 'java'. You may also want to use ps's -f flag to print the full command name of each listed process. That way, you can see what each java process is actually doing. Here is the command in full: ps -fC java.

You could also use pgrep to list all java processes. pgrep -a java will return the PID and full command line of each java process.

Once you have the PID of the command you wish to kill, use kill with the -9 (SIGKILL) flag and the PID of the java process you wish to kill. Java doesn't always stop when it receives a 'SIGTERM' signal (processes are allowed to handle 'SIGTERM'), so sending it the 'SIGKILL' signal, which makes init kill the program without warning it first, is often necessary.

For example, if ps -fC java returns

UID        PID  PPID  C STIME TTY          TIME CMD
jeff      9014  8890  0 08:51 pts/0    00:00:00 java IDE
jeff     11775  8890  6 08:59 pts/0    00:00:00 java TestProgram

or psgrep -a java returns

9014 java IDE
11775 java TestProgram

and you wish to kill java TestProgram, you should run kill -9 11775.

share|improve this answer
add comment

IMO the best solution is:

pkill -9 -f <nameOfYourJavaAplication>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.