I have a requirement to write to a log file on reception of any terminate command like SIGTERM AND SIGKILL.
I can register for SIGTERM but how can handle the SIGKILL signal?
The SIGKILL signal is used to cause immediate program termination. It cannot be handled or ignored, and is therefore always fatal. It is also not possible to block this signal. This signal is usually generated only by explicit request.
SIGTERM vs SIGKILL:SIGTERM gracefully kills the process whereas SIGKILL kills the process immediately. SIGTERM signal can be handled, ignored and blocked but SIGKILL cannot be handled or blocked. SIGTERM doesn't kill the child processes. SIGKILL kills the child processes as well.
SIGKILL. (signal 9) is a directive to kill the process immediately. This signal cannot be caught or ignored. It is typically better to issue SIGTERM rather than SIGKILL. If the program has a handler for SIGTERM, it can clean up and terminate in an orderly fashion.
You cannot, at least not for the process being killed.
What you can do is arrange for the parent process to watch for the child process's death, and act accordingly. Any decent process supervision system, such as daemontools, has such a facility built in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With