Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

and

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

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

share|improve this question
    
I don't get it, you have a PID right there, the second column –  warl0ck Apr 17 '13 at 7:40
    
I don't know which is which though. –  Peter Wood Apr 17 '13 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 Apr 17 '13 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. –  Peter Wood Apr 17 '13 at 18:23
add comment

3 Answers

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
do
    java -jar MyApp.jar &
    my_app_pid=$!
    while ! whatever_makes_me_think_my_app_should_be_restarted
    do
        sleep 60
    done
    kill $my_app_pid
done
share|improve this answer
    
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. –  Peter Wood Apr 17 '13 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. –  Peter Wood Apr 17 '13 at 13:02
add comment

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'
PID=20936
tr '\0' '\n' </proc/"$PID"/environ | grep IDSTRING
IDSTRING=PROC1

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.

share|improve this answer
    
So exec -a passes mysleep as the zeroth argument to sleep. That's useful. I'll have to work out how to make it work with jar files. Thanks. –  Peter Wood Apr 17 '13 at 8:30
add comment

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

share|improve this answer
    
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. –  Peter Wood Apr 17 '13 at 8:32
5  
How can you claim anything that is so obviously wrong? –  Hauke Laging Apr 17 '13 at 8:35
add comment

Your Answer

 
discard

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.