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 :P

Edit: I am using ubuntu 15.04.

  • are you using openjdk? – Maythux Jul 24 '15 at 10:21
  • no it is java-7-oracle – Atiq Jul 24 '15 at 10:23
  • check my answer – Maythux Jul 24 '15 at 10:24
up vote 17 down vote accepted

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

  • Oh great it's working...Thanks. – Atiq Jul 24 '15 at 10:26
  • @Atiq thanks, have a nice time – Maythux Jul 24 '15 at 10:34
  • 3
    ++1 for not using -9 indiscriminately. You should ever give a process the chance to exit gracefully. – Rmano Jul 24 '15 at 11:02
  • @Rmano thank you for your note :) – Maythux Jul 24 '15 at 11:03
  • 2
    @abligh The problem is with pgrep you are not able to distinguish which processes are running eclipse, because even pgrep -l will just report java. @ Maythux, just for reference, egrep is deprecated and should not be used, since grep -E does exactly the same, although you don't even need grep -E since you're matching a fixed string; so just grep -v "eclipse" (or if you want to be fancy [...] | grep -F "java" | grep -vF "eclipse | [...]") – kos Jul 24 '15 at 11:58

Using awk

ps ax | awk '/java/ && !/eclipse/ {system("kill "$1)}'

The command kills all java processes, but not the process eclipse.

  • 3
    More elegant (and faster) then the answer from @maythux IMHO... ;-) – Fabby Jul 24 '15 at 11:26
  • I agree with Fabby, nice! – kos Jul 24 '15 at 12:01

How about :

pgrep -af 'java' | grep -Po '^[^ ]+(?!.*eclipse)' | xargs kill
  • pgrep -af 'java' searches for java in the full command line of the processes and list them

  • grep -Po '^[^ ]+(?!.*eclipse)' looks among the searched processes and get the process IDs of the processes that do not have eclipse in their full command lines

  • xargs kill will kill the processes.

  • +1 for a short and elegant solution. Note that it would have been possible to have an even more efficient (only one command: pkill) solution if pgrep/pkill supported perl regex'es which you use in the 2nd step above. Unfortunately pgrep/pkill only support extended-regexps (like egrep/grep -E). – arielf Jul 24 '15 at 20:49
  • @arielf You are absolutely right..it would have been most efficient if pgrep/pkill supports PCRE.. – heemayl Jul 24 '15 at 20:54

jps -m | grep -v eclipse | awk '{print $1}' | xargs -t kill

Use jps to make sure that you are looking for just the java commands and not all the commands which have java in their path.

ps ax | grep "java" | egrep -v "eclipse" | cut -b1-06 | xargs -t kill
  • 1
    It would have been nicer to explain the commands, rather that just give a cryptic command string. Also, what about my long-running command "orkjavagoob.sh" – waltinator Jul 25 '15 at 0:55

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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