Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a SIGTERM

Tags:

java

exit

sigterm

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

like image 721
Martijn Courteaux Avatar asked Jun 04 '10 14:06

Martijn Courteaux


People also ask

Can SIGTERM be handled?

The SIGTERM signal is a generic signal used to cause program termination. Unlike SIGKILL , this signal can be blocked, handled, and ignored.

How do you process SIGTERM signal gracefully?

The question was how to gracefully quit on SIGTERM. Your example shows how to ignore SIGINT. You can change to ignore SIGTERM, but it is a bad idea for a daemon to ignore it, because it will then instead be SIGKILL:ed after a timeout, and that can't be ignored or gracefully handled at all.

What happens when a process receives SIGTERM?

If a process receives SIGTERM, some other process sent that signal. SIGTERM is the signal that is typically used to administratively terminate a process. That's not a signal that the kernel would send, but that's the signal a process would typically send to terminate (gracefully) another process.

Does SIGTERM kill child process?

The SIGKILL signal can be sent with a kill system call. Note though, the SIGTERM handler registered in the following code sample can't catch the delivered SIGKILL , and it immediately kills the given child process.


1 Answers

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

like image 76
Matthew Flaschen Avatar answered Oct 13 '22 06:10

Matthew Flaschen