views:

77

answers:

3

Hi,

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?

+5  A: 

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.

Chris Jester-Young
Using the parent process is easiest, but not required. Any process that holds the other end of a pipe or fifo will be notified when its peer is killed.
Ben Voigt
@Ben: The pipe can be closed via some other means than the process's death, and so isn't quite as reliable. But if you don't control the parent process, that may be the only option you have. (daemontools has a `fghack` program that works on that premise.)
Chris Jester-Young
A: 

You can have a look at this project - it may offer a way.

slashmais
Nope, no matter how fancy they get, they still ain't catching SIGKILL, not unless they offer a custom kernel module or something.
Omnifarious
was a thought - I used segvcatch in a app or two, then realized that it doesn't really beat diligence when programming, actually ends up making you lazy and waiting for the error-log to find )
slashmais
+3  A: 

Only Doug McIlroy can handle SIGKILL. Doug McIlroy's facts.

ninjalj