Tuesday, February 19, 2013

JVMs and kill signals

Ever wondered how a JVM reacts to various kill signals? The (intended) behaviour might be documented somewhere already, but I found having a table of the actual behaviour available quite useful. In particular I wanted to know which kill signals trigger the JVM to run registered shutdown hooks, and which kill signals don't actually terminate the JVM. So I decided to compile a table of that information.

I wrote up a small Java application that just registers a shutdown hook that I can detect whether it has executed or not, and then sleeps until I get a chance to kill it:

class Death {
  public static void main(String... args) throws Exception {
    Runtime.getRuntime().addShutdownHook( new Thread(){
      @Override
      public void run()
      {
        System.out.println("Shutting down");
      }
    } );
    for (;;) Thread.sleep(100);
  }
}

Then I ran the program in one terminal window (java Death; echo $?) while iterating through all kill signals (0-31) in another:

kill -$SIGNAL $(jps | grep Death | cut -d\  -f1)
signalshutdownruns hookexit codecomment
default (15)yesyes143SIGTERM is the default unix kill signal
0no--
1 (SIGHUP)yesyes129
2 (SIGINT)yesyes130SIGINT is the signal sent on ^C
3 (SIGQUIT)no--Makes the JVM dump threads / stack-traces
4 (SIGILL)yesno134Makes the JVM write a core dump and abort on trap 6
5yesno133Makes the JVM exit with "Trace/BPT trap: 5"
6 (SIGABRT)yesno134Makes the JVM exit with "Abort trap: 6"
7yesno135Makes the JVM exit with "EMT trap: 7"
8 (SIGFPE)yesno134Makes the JVM write a core dump and abort on trap 6
9 (SIGKILL)yesno137The JVM is forcibly killed (exits with "Killed: 9")
10 (SIGBUS)yesno134Emulates a "Bus Error"
11 (SIGSEGV)yesno134Emulates a "Segmentation fault"
12yesno140Makes the JVM exit with "Bad system call: 12"
13no--
14yesno142Makes the JVM exit with "Alarm clock: 14"
15 (SIGTERM)yesyes143This is the default unix kill signal
16no--
17no-145Stops the application (sends it to the background), same as ^Z
18no-146Stops the application (sends it to the background), same as ^Z
19no--
20no--
21no-149Stops the application (sends it to the background), same as ^Z
22no-150Stops the application (sends it to the background), same as ^Z
23no--
24yesno152Makes the JVM exit with "Cputime limit exceeded: 24"
25no--
26yesno154Makes the JVM exit with "Virtual timer expired: 26"
27yesno155Makes the JVM exit with "Profiling timer expired: 27"
28no--
29no--
30yesno158Makes the JVM exit with "User defined signal 1: 30"
31yesno134Makes the JVM exit on Segmentation fault

This list was compiled using (a quite old) Oracle Hotspot Java 8 EA on Mac OS X:

java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b65)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b09, mixed mode)

Hope this is useful to more people than myself.

4 comments:

Unknown said...

That's very useful! I'll keep this as reference, thanks for putting in the work, Tobias.

Amit G said...

JVM exit due to specific signal would be
128+signal-id
List of signal-id can be found using `kill -l`

Anonymous said...

Very useful

Thanks.

Mykhaylo Adamovych said...

Advanced Bash-Scripting Guide: Exit Codes With Special Meanings
http://tldp.org/LDP/abs/html/exitcodes.html