kill and kill -9
Java provides a way to add a shutdown hook that will be called during the VM's exit sequence and semantics of that are clearly defined in the Javadoc here.
The Javadoc clearly instructs If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.
I was just curious to test this out with GlassFish Application Server on my MacBookPro. When the GlassFish server VM either aborts or shuts down, I observe the following:
|signal-id||meaning according to manpage||observation|
|Default (no-id, 15)||TERM (software termination signal)||Shutdown hook called.|
|1||HUP (hang up)||Shutdown hook called, VM exits.|
|2||INT (interrupt)||Shutdown hook called, VM exits.|
|3||QUIT (quit)||Shutdown hook NOT called, VM continues to run. This is understandable as this should result in VM spitting the thread dump. In other words, this signal is handled at the VM level and Java code does not get a chance to do anything. :)|
|6||ABRT (abort)||Shutdown hook NOT called, VM exits.|
|9||KILL (thou shalt die NOW)||Shutdown hook NOT called, VM exits.|
|14||ALRM (alarm clock)||Shutdown hook NOT called, VM exits.|
|15||See the default case above.||See default case above.|