Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture SIGINT in Java

Tags:

java

signals

People also ask

How do you handle SIGINT in Java?

The way to handle this would be to register a shutdown hook. If you use (SIGINT) kill -2 will cause the program to gracefully exit and run the shutdown hooks. Registers a new virtual-machine shutdown hook.

How does JVM handle Sigterm?

The SIGTERM signal requests termination, whereby SIGKILL ...is sent to a process to cause it to terminate immediately... ...is executed for SIGTERM and SIGINT. The JVM executes the hook and waits, in the case above for 10 seconds, with the shutdown procedure until the completion of the shutdownHook .

How do I use Ctrl C in Java?

The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System. exit) method is invoked, or. The virtual machine is terminated in response to a user interrupt, such as typing Ctrl + C , or a system-wide event, such as user logoff or system shutdown.

What does SIGINT do in C?

SIGINT is one of the predefined signals that's associated with the terminal interrupt character (commonly Ctrl + C ). It causes the shell to stop the current process and return to its main loop, displaying a new command prompt to the user.


The way to handle this would be to register a shutdown hook. If you use (SIGINT) kill -2 will cause the program to gracefully exit and run the shutdown hooks.

Registers a new virtual-machine shutdown hook.

The Java virtual machine shuts down in response to two kinds of events:

  • The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or

  • The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

I tried the following test program on OSX 10.6.3 and on kill -9 it did NOT run the shutdown hook, didn't think it would. On a kill -15 it DOES run the shutdown hook every time.

public class TestShutdownHook
{
    public static void main(final String[] args) throws InterruptedException
    {
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {
                System.out.println("Shutdown hook ran!");
            }
        });

        while (true)
        {
            Thread.sleep(1000);
        }
    }
}

This is the documented way to write your own signal handlers that aren't shutdown hooks in Java. Be warned that the com.sun.misc packages and are un-supported and may be changed or go away at any time and probably only exist in the Sun JVM.


Since at least Java 6, the HotSpot java command has had a -Xrs flag to disable the JVM signal handlers.

If you use it, you need to implement your own SIGINT, SIGTERM, SIGHUP, and SIGQUIT handlers, including one to explicitly call System.exit().

Note: this means the thread dump facility of SIGQUIT is disabled when the -Xrs flag is present.

See https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html#BABHDABI (also available on the Windows and Solaris java counterparts).


Take a look at Integrating Signal and Exception Handling describing the HotSpot JVM's ability do do signal chaining. It does involve some C-level trickery though.

Wouldn't java.lang.Runtime.addShutdownHook(Thread) be enough for your purpose? These hooks are run by the JVM upon termination, including reception of SIGINT, as long as the JVM is able to perform them.