Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIGKILL signal Handler

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?

like image 495
Sirish Avatar asked Oct 11 '10 17:10

Sirish


People also ask

Can SIGKILL be handled?

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.

What is the difference between SIGTERM and SIGKILL?

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.

What does SIGKILL mean?

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.


1 Answers

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.

like image 50
Chris Jester-Young Avatar answered Oct 01 '22 10:10

Chris Jester-Young