Amended:
In short, the OS kernel does not care if the process is killed. That is dependant on whether the process logs anything. All the kernel cares about at this stage is reclaiming memory. But read on, on how to catch it and log it...
As per caf and Stephen C's mention on their comments...
- If you are running BSD accounting daemon module in the kernel, everything gets logged. Thanks to Stephen C for pointing this out! I did not realize that functionality as I have this switched off/disabled.
- In my hindsight, as per caf's comment - the two signals that cannot be caught are SIGKILL and SIGSTOP, and also the fact that I mentioned
atexit
, and I described in the code, that should have been exit(0);
..ooops Thanks caf!
Original
The best way to catch the kill signal is you need to use a signal handler to handle a few signals , not just SIGKILL
on its own will suffice, SIGABRT
(abort), SIGQUIT
(terminal program quit), SIGSTOP
and SIGHUP
(hangup). Those signals together is what would catch the command kill
on the command line. The signal handler can then log the information stored in /var/log/messages
(environment dependant or Linux distribution dependant). For further reference, see here.
Also, see here for an example of how to use a signal handler using the sigaction
function.
Also it would be a good idea to adopt the usage of atexit
function, then when the code exits at runtime, the runtime will execute the last function before returning back to the command line. Reference for atexit
is here.
When the C function exit
is used, and executed, the atexit
function will execute the function pointer where applied as in the example below. - Thanks caf for this!
An example usage of atexit
as shown:
#include <stdlib.h>
int main(int argc, char **argv){
atexit(myexitfunc); /* Beginning, immediately right after declaration(s) */
/* Rest of code */
return 0;
exit(0);
}
int myexitfunc(void){
fprintf(stdout, "Goodbye cruel world...\n");
}
Hope this helps,
Best regards,
Tom.