Ubuntu – How to kill all processes apart from specific one

command lineprocess

On my local server, many java processes are running, and at the same time I am using Eclipse. Now when I need to restart my processes (I start processes through a shell script), before every start I need to kill the old processes through:

Method 1:-
kill -9 $Pid (for each process one by one, which is time-consuming)

Method 2:-
killall -9 java (for all processes – easy, but Eclipse will be killed too unnecessarily)

In the case of Method 2 Eclipse will also be killed, which I never want. Is there any way out of this? I bet there must be 😛

Edit: I am using ubuntu 15.04.

Best Answer

Give a try to this command:

ps ax | grep "java" | egrep -v "eclipse" | cut -b1-06 | xargs -t kill

this will search for all processes containing java and execluding eclipse then kill them

Related Question