I have a project that should be run forever under unix. But in case we need to stop it, i am thinking about writing an termination program to close it gracefully. I have no experience in writing this kind of program. Could anyone please give me some hint. Is there any signal catching scheme in java? Thank you
-
1possible duplicate of [How to trap a SIGNAL in a java application initialized using a bash script](http://stackoverflow.com/questions/4147288/how-to-trap-a-signal-in-a-java-application-initialized-using-a-bash-script) – hvgotcodes Jan 03 '12 at 17:59
4 Answers
You need to add a shutdown hook to your application. This is a thread which will be started when your program exits normally or if the JVM is terminated in response to a user interrupt, SIGTERM signal or a system-wide event, such as user logoff or system shutdown. Take a look at the javadocs for more information.
Here are the steps involved when creating shutdown hooks:
1) Create the shutdown hook class:
public class AppShutdownHook extends Thread{
public void run(){
logger.info("Running shutdown hook...") ;
//cleanup e.g. close database connections etc
}
}
2) Register the shutdown hook:
public class App{
public App(){
Runtime.getRuntime().addShutdownHook(new AppShutdownHook()) ;
}
}
The beauty of this approach is that you don't have a special "stop script" which you need to remember to run every time you have to stop your application or reboot your machine.
Reference: Shutting Down Java Apps

- 266,786
- 75
- 396
- 414
There are a couple of options here.
One is that you could have it accept command line input, but that would require you have access to it's input stream which may not happen if you close the terminal etc. What is probably the best way to do this is to use either a socket and have it listen for a specific stream in order to shut down. Your termination program would then be responsible for writing to that socket.
Another option would be to use some sort of more complex IPC like shared memory.
And finally another option would be to use a flat file stored somewhere on the system that your process would monitor for changes. The termination program would then write to that file.

- 35,167
- 12
- 80
- 109
I remembered a similar question being asked time ago. One possible course of action is the use of SignalHandler.
You can read the full article here. It appears to be related to IBM JVM but I think it is equally valid for Java Hotspot.
A little-known feature of Java is the ability of an application to install its own signal handler, which is supported through the sun.misc.Signal class. However, use caution when using classes from the sun.misc package because it contains undocumented support classes that may change between releases of Java. You can install a Java handler for any signal that is not used by the JVM. These signal handlers are similar to native handlers because they're invoked when a native system signal is raised, but they will always run as a separate Java thread. Essentially, when a signal is raised for which a Java signal handler is available, the JVM's "signal dispatcher thread" is woken up and informed of the signal. The signal dispatcher thread then invokes a Java method to create and start a new thread for the installed Java signal handler. To write a Java signal handler, define a class that implements the sun.misc.SignalHandler interface and register the handler by using the sun.misc.Signal.handle() method.
I had also participated in a discussion about the subject here

- 1
- 1

- 76,803
- 25
- 144
- 205
-
oops... didn't see your answer when I posted mine... I checked both ibm and sun jdk6 installations that I have and both still seem to support this feature. – Rob H Jan 03 '12 at 18:27
It is possible to do signal handling in java but it is a feature that relies on using sun.misc.* classes which is something to be approached with caution since they are considered internal classes and subject change between releases.
With that caveat, check this link for more information: http://www.ibm.com/developerworks/ibm/library/i-signalhandling/#N102C8
This article is based on java 1.3.1 but the mechanism is still present in both the sun and ibm 1.6 jvms.
I wonder too if it's feasible to use jni and agentlib to install a native signal handler in your jvm... I've never tried it and would have to look into it. It's possible the jvm already has signal handling that cannot be overridden.

- 1,676
- 10
- 18