Is it safe to send SIGTERM to JVM

java, jvm, scala, shutdown-hook, signals

Solution

As long as Java service uses ShutdownHooks wisely for orderly termination, there is no problem in sending SIGTERM to JVM process. E.g. we use SIGTERM as a primary method for initiating an application shutdown in our large high-load production system (5000+ servers running 80 different Java applications).

Problem

Although JVM will translate SIGTERM and similar signals to shutdown hooks, many service shutdown scripts use a TCP port to initiate a shutdown. (e.g. Tomcat's shutdown port, Java Service Wrapper, JBoss' management interfaces, etc.) So I thought using signals and shutdown hooks to gracefully shutdown java services is discouraged, until I found that Play! framework is managing the service lifecycle with shutdown hooks and the startup scripts generated by `play dist` assumes that a signal will be sent to the JVM's PID. I know that signals are platform-dependent and using a TCP port is a simple and extensible way to manage services in a cross-platform manner, but I would like to know how safe it is and what risks I need to consider, when I rely on SIGTERM and shutdown hooks as a primary method for service shutdown.

Original source