1

I have a shell script, which is run under a opensuse linux, that starts a java application (under a jar), the script is:

#!/bin/sh

#export JAVA_HOME=/usr/local/java
#PATH=/usr/local/java/bin:${PATH}

#---------------------------------#
# dynamically build the classpath #
#---------------------------------#
THE_CLASSPATH=
for i in `ls ./lib/*.jar`
do
  THE_CLASSPATH=${THE_CLASSPATH}:${i}
done

#---------------------------#
# run the application #
#---------------------------#
java -server -Xms512M -Xmx1G -cp ".:${THE_CLASSPATH}"  com.package.MyApp > myApp.out 2>&0 &   

This script is working fine.

Now, what I want, is to write a script to kill gracefully this app, something that allows me to kill it with the -15 argument from Linux kill command.

The problem, is that there will be many java applications running on this server, so I need to specifically kill this one.

Any help?

Thanks in advance,

Fernando

1

3 Answers 3

4

Store the process ID in a file:

java ...blabla... &
echo $! > MyApp.pid

Then you can kill the JVM with kill -TERM $(cat MyApp.pid).

Alternatively you can list the java processes on your system with jps. It lists the process ID and mainclass or jar file used to launch the application, which is probably enough to recognize your application. For example:

$ jps
20168 MyAPp
20186 Jps
$ kill -TERM 20168

More info on jps: http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jps.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Joni Salonen, but I can't use jps. However it was very helpful.
3

Write the PID of the backgrounded app somewhere useful.

echo $! > /var/lib/MyApp.pid

Then kill that PID in your other script.

1 Comment

Thanks @ignacio-vazquez-abrams, the resto of the answer was found here: stackoverflow.com/questions/8884480/…
0

When you use ps xa you can get the list of running processes including the full command used to start it. If you do not want to use a temporary file like the others have mentioned, you can grep for java and also your class or jar name.

ps xa | grep java | grep com.package.MyApp | xargs kill -15

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.