Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 3 instance of solr running

  1. using tomcat (in shell u will find something like -Dcatalina.base=/usr/local/apache-tomcat-6.0.35 -)
  2. suing tomcat_1 (in shell u will find something like -Dcatalina.base=/usr/local/apache-tomcat-6.0.35_1 -)
  3. using tomcat_2 (in shell u will find something like -Dcatalina.base=/usr/local/apache-tomcat-6.0.35_2 -)

Can i write a shell script which can kill the particular instance

  1. for 1 : kill tomcat but not tomcat_1 & tomcat_2
  2. for 2 : kill tomcat_1 but not tomcat & tomcat_2
  3. for 3 : kill tomcat_2 but not tomcat & tomcat_1
share|improve this question

migrated from Dec 8 '12 at 22:44

This question came from our site for professional and enthusiast programmers.

Sure you can. What have you tried? – thkala Dec 8 '12 at 17:19

You could list all running processes using ps, search for a substring which uniquely identifies the process you want to kill (e.g. 'tomcat-6.0.35 ' or 'tomcat-6.0.35_1 '), extract the PID from the matching line and kill just the process with that specific PID.

ps -Af | grep 'tomcat-6.0.35 ' | grep -v 'grep' | sed 's/^[^[:space:]]\+[[:space:]]\+\([[:digit:]]\+\)[[:space:]].*$/\1/g' | xargs kill

So, what this does in detail:
List all processes, select only those matching your search pattern (the trailing space is necessary if you don't want it to match 'tomcat-6.0.35_1' as well), filter out the script itself, replace the remaining lines with only the number in the second column (which is the PID), then run kill for each of these PIDs.
You might want to use kill -9 if you're really nasty.

share|improve this answer

Your Answer


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.