tags:

views:

186

answers:

3

No many words needed:

Is there a way in Java to handle a received SIGTERM?

+4  A: 

Yes, you can register a shutdown hook with Runtime.addShutdownHook().

Matthew Flaschen
Thanks. Exactly what I was searching for.
Martijn Courteaux
+1  A: 

You could add a shutdown hook to do any cleanup.

scompt.com
A: 

Another way to handle signals in Java is via the sun.misc.signal package. Refer to http://www.ibm.com/developerworks/java/library/i-signalhandling/ for understanding how to use it.

NOTE: The functionality being within sun.* package would also mean that it may not be portable/behave-the-same across all OS(s). But you may want to try it out.

arcamax
@arcamax: But in fact, it does the same?
Martijn Courteaux
Nope - not exactly. The signal handling API I mentioned can be used to handle most of the Native OS signals (trap, intr, kill, etc) within Java Apps.On the other hand ShutdownHook gets invoked only when JVM is finally terminating (after a kill signal is received).
arcamax