I have got ps aux | grep -v 'grep' | grep "java" | awk '{print $2}' command that can filter all java processes but the problem I have is I want to select only those process whose name contains core or nimbus. There are other java processes like eclipse and mail server which are running at the same time and I don't want them to be there.

I have checked it from Kill Java processes.

I am new to scripting, so please pardon me if the question is silly. Thanks!

pkill -f 'java.*(core|nimbus)'

I don't have a Java setup like yours but I do run a load of Django applications on my server. Similar situation in that they all look like UWSGI processes. Here's a chunk of ps aux | grep uwsgi:

oli       7201  0.0  1.0  70324 20936 ?        S    12:54   0:00 /web/venv/bin/uwsgi --include /web/django-uwsgi.ini --ini /web/asvsforms/uwsgi.ini
oli       7202  0.0  1.0  70336 20956 ?        S    12:54   0:00 /web/venv/bin/uwsgi --include /web/django-uwsgi.ini --ini /web/bnc/uwsgi.ini
oli       7203  0.0  1.0  70456 21016 ?        S    12:54   0:00 /web/venv/bin/uwsgi --include /web/django-uwsgi.ini --ini /web/cleanup/uwsgi.ini
oli       7204  0.0  1.0  70272 21068 ?        S    12:54   0:00 /web/venv/bin/uwsgi --include /web/django-uwsgi.ini --ini /web/emwill/uwsgi.ini
oli       7205  0.0  1.3  88336 28124 ?        S    12:54   0:00 /web/venv/bin/uwsgi --include /web/django-uwsgi.ini --ini /web/qi/uwsgi.ini
oli       7206  0.0  1.0  70256 20996 ?        S    12:54   0:00 /web/venv/bin/uwsgi --include /web/django-uwsgi.ini --ini /web/sbaccuracy/uwsgi.ini

You can awk that down (you don't need grep and awk) but jave a look at pgrep!

$ pgrep -af 'uwsgi.*cleanup'
7203 /web/venv/bin/uwsgi --include /web/django-uwsgi.ini --ini /web/cleanup/uwsgi.ini
7217 /web/venv/bin/uwsgi --include /web/django-uwsgi.ini --ini /web/cleanup/uwsgi.ini

It's the -f which is really required here. This expands the pattern search to the whole command line used to run something. Without it it'd only be searching the first part. The -a is just here to output the full line.

Anyway, to kill that, we just switch to pkill which is practically the same command. It doesn't have the same output features but that's not what it's designed for. And that leads us back to the top of the answer.

If you're going to insist on filtering ps, please don't use ps|grep|grep|awk|egrep|awk|xargs kill or whatever has now been suggested. It just looks silly. A single awk can do all the filtering.

ps aux | awk '/java.*(core|nimbus)/ {print $2}'

Additionally, you can send the list to kill from directly within awk:

ps aux | awk '/java.*(core|nimbus)/ {print $2 | kill}'

I'd still pick pgrep and pkill though.

  • The "looks silly" statement is obviously subjective, but, other than that, your answer is top-notch. Cheers. – Piotrek Oct 1 '15 at 16:11

Almost there. To select the processes you can do this:

ps aux | grep -v 'grep' | grep "java" |egrep -E "core|nimbus"

And when that gives the processes you need, you can proceed to kill them all in one go:

ps aux | grep -v 'grep' | grep "java" | egrep -E "core|nimbus" | awk '{print $2}' |xargs -n 1 kill


Almost there! Try this:

ps aux | grep -v 'grep' | grep "java" | awk '/core/ || /nimbus/ {print $2;}'

You can then kill the processes doing something like Maarten posted, below, or this:

kill -9 `ps aux | grep -v 'grep' | grep "java" | awk '/core/ || /nimbus/ {print $2;}'`
  • Why twice grep? You don't need grep if you use awk – A.B. Oct 1 '15 at 15:52
  • The first grep (-v) makes sure that this command is excluded from the output. The second grep (java) makes sure that you only get processes with the word java in them. I think the second one is necessary because there may be other processes with core or nimbus in their names, and you don't want to kill those. – Piotrek Oct 1 '15 at 16:03
  • I think the point they were making is you could do the searching that the two greps are doing from within the awk. – Oli Oct 1 '15 at 16:06
  • @Oli: Granted, I may have misunderstood the comment. Your answer contains the corresponding details - thanks. – Piotrek Oct 1 '15 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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