Hi folks,
Using shell script I'm running two java applications at background.

The output of 'ps -ef' is
UID PID PPID TTY STIME COMMAND
Amar 2388 1 con 15:35:58 /usr/bin/bash
Amar 1272 1 con 15:44:29 /cygdrive/c/Program Files/Java/jdk1.6.0_01/bin/java
Amar 3972 1 con 15:44:55 /cygdrive/c/Program Files/Java/jdk1.6.0_01/bin/java
Amar 1600 2388 con 15:46:12 /usr/bin/ps

Now I want to kill one Java application from above. How to do that one? How I know at run time which PID is for which application?

Recommended Answers

All 5 Replies


After running it, save the PID:

java xxxx &
j1pid=$!
java yyyy &
j2pid=$!

Then you can kill "$j1pid" or kill "$j2pid" .

Hey Chris,
Thank you very much.
It works !! Great.

Also one more thing..
Can I print the name of running java application using shell script?

Hey Chris,
Thank you very much.
It works !! Great.

Also one more thing..
Can I print the name of running java application using shell script?

What do you mean "name"? The "name" is Java. Another thing you could do (depends on how flexible your organisation is about this), is to create links to java with different names, thereby allowing the program to run under a different "name" than java.

I.E. create a /user/java link that always points to the "current" version of java. Then create another directory such as, say, /user/jprogs. Then, in that directory create links to the java command. So, say you have an application under the project name "someApp". You do

ln -s /usr/java/jre/bin/java /user/jprogs/someApp

(assuming /usr/java points to a jdk, if it points to a jre then remove the "jre" from above").

In this way your app processes each have a distinictive name without having to redo links (except for the /usr/java link) when you upgrade your jdk/jre.

It also makes it easy to switch from one version of java to another easily. Say you upgrade your jdk. Normally, you then have to go through all your apps and make sure they are using the "new" version. Then there is a problem and you want to "downgrade" back to the old version. Now you have to go through all the apps again. With the /usr/java link, you simply have to make sure all apps are configured with that, and then you can change the link at will to switch from version to version.

@masijade
Thanks for your quick reply.

If you saw my 1st post in this thread,
there I gave output of 'ps -ef'

now name means...
Suppose I'm running app1.java and app2.java at the background simultaneously.
But in output of 'ps -ef' it is showing only two java processes are running.
How can I get/print 'app1' and 'app2' using shell script?

What do you mean "name"? The "name" is Java. Another thing you could do (depends on how flexible your organisation is about this), is to create links to java with different names, thereby allowing the program to run under a different "name" than java.

I.E. create a /user/java link that always points to the "current" version of java. Then create another directory such as, say, /user/jprogs. Then, in that directory create links to the java command. So, say you have an application under the project name "someApp". You do

ln -s /usr/java/jre/bin/java /user/jprogs/someApp

(assuming /usr/java points to a jdk, if it points to a jre then remove the "jre" from above").

In this way your app processes each have a distinictive name without having to redo links (except for the /usr/java link) when you upgrade your jdk/jre.

It also makes it easy to switch from one version of java to another easily. Say you upgrade your jdk. Normally, you then have to go through all your apps and make sure they are using the "new" version. Then there is a problem and you want to "downgrade" back to the old version. Now you have to go through all the apps again. With the /usr/java link, you simply have to make sure all apps are configured with that, and then you can change the link at will to switch from version to version.

Did you try out my suggestions in that post? Otherwise, have fun, because for one, you have to hop that the complete command, with options to the vm, is not so long that the class name isn't even retained in the ps listing. And, if it is, you need to parse that that line and using cut, sed, awk, or something similar try to cut out the "name" and differentiate it from the vm arguments, as well as any arguments that might have been fed into it. As the number of arguments (both to the VM and the app) can, obviously, vary, there is no "surefire" way of doing this in a "non-complex" generic manner.

Then again, since you're controlling it from the script that starts it, that "xxx" from the first reply is your name, so what else do you want?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.