I have two java processes which run using the same file name, MyApp.jar (for example).

/usr/java/latest/bin/java -jar MyApp.jar

These jars exist in different places and use different configurations.

I want to be able to kill one process, but don't know how to distinguish between the two. I execute:

 ps aux | grep [M]yApp.jar

And get:

admin    21509  0.8  0.1 1199908 20484 ?       Sl   08:21   0:00 /usr/java/latest/bin/java -jar MyApp.jar
admin    21585  6.7  0.1 1199764 20084 ?       Sl   08:21   0:00 

I've thought of creating the process with some dummy parameter to be able to distinguish them:

/usr/java/latest/bin/java -jar MyApp.jar MyAppTheFirst


/usr/java/latest/bin/java -jar MyApp.jar MyAppTheSecond

But this seems a bit wrong to me. Is there a better way?

  • I don't get it, you have a PID right there, the second column
    – daisy
    Commented Apr 17, 2013 at 7:40
  • I don't know which is which though. Commented Apr 17, 2013 at 7:54
  • @PeterWood Which is which? If you can't tell them apart, what makes one deserving of death more than the other?
    – depquid
    Commented Apr 17, 2013 at 16:33
  • @depquid They are configured differently and performing different tasks. Sometimes one may need restarting with a new configuration, or with a new version of the jar, or needs stopping as the service it is using is down and the logs are just filling up, etc. Commented Apr 17, 2013 at 18:23

3 Answers 3


First, why do you want to kill any of them? If a process is using too many resources you can just get the PID in top:

top -n 1

If they need to be restarted on a periodic basis or to react to outside stimuli, use process management:

while true
    java -jar MyApp.jar &
    while ! whatever_makes_me_think_my_app_should_be_restarted
        sleep 60
    kill $my_app_pid
  • I have many processes which have control scripts in /etc/init.d/ which I can just say stop, start, restart. I recently deployed a new one which reused an existing jar and but it kills the wrong process. The process management link provided shows me that I'm probably doing it all wrong, but I have to fit into the system, or at least, it's easier if I do, even if it's not ideal. Hmm, people and processes... it is an XY problem. Commented Apr 17, 2013 at 11:22
  • My colleague helped me solve this. We now pass a unique system property to java using -D, and we can recover this with a grep. I like both the top answers as you've both helped me in different ways. Yours was more educational, the other more on task. I like questioning the cause of the problem too, and appreciate you pulling that on me. Commented Apr 17, 2013 at 13:02

Changing parameters makes a difference to the application thus it's not the best idea. You could change $0 i.e. the program name itself. Most programs don't care about that but some change their behaviour based on that (e.g. if you can call the same binary by different names like IIRC with mtools and busybox):

bash -c 'exec -a mysleep sleep 100'
ps aux | grep sleep
hl       20818  0.0  0.0   5732   576 pts/10   S+   10:01   0:00 mysleep 100

or you use the environment:

bash -c 'IDSTRING=PROC1 exec sleep 100'
tr '\0' '\n' </proc/"$PID"/environ | grep IDSTRING

or you start the process with a wrapper script which writes the PID and additional information (for telling the processes apart) into a log file.


The only way which can help is to determine the bigger PID number.. the bigger one is the later ran process..BUT NOT ALWAYS

  • I'm running a server, and processes are started and stopped at various times by various people. I don't know which will be more recent. Commented Apr 17, 2013 at 8:32
  • 5
    How can you claim anything that is so obviously wrong? Commented Apr 17, 2013 at 8:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .