1,105,312 Community Members

How To Kill Java Applications using Shell Script

Member Avatar
ahp@aol.in
Light Poster
27 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
cfajohnson
Junior Poster
199 posts since Dec 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 24 [?]
Skill Endorsements: 0 [?]
 
0
 


After running it, save the PID:

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

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

Member Avatar
ahp@aol.in
Light Poster
27 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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.

Member Avatar
ahp@aol.in
Light Poster
27 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@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.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article