How do you handle clean up when the program receives a kill signal?
For instance, there is an application I connect to that wants any third party app (my app) to send a finish command when logging out. What is the best say to send that finish command when my app has been destroyed with a kill -9?
edit 1: kill -9 cannot be captured. Thank you guys for correcting me.
edit 2: I guess this case would be when the one calls just kill which is the same as ctrl-c
It is impossible for any program, in any language, to handle a SIGKILL. This is so it is always possible to terminate a program, even if the program is buggy or malicious. But SIGKILL is not the only means for terminating a program. The other is to use a SIGTERM. Programs can handle that signal. The program should handle the signal by doing a controlled, but rapid, shutdown. When a computer shuts down, the final stage of the shutdown process sends every remaining process a SIGTERM, gives those processes a few seconds grace, then sends them a SIGKILL.
The way to handle this for anything other than
kill -9would be to register a shutdown hook. If you can use (SIGTERM)kill -15the shutdown hook will work. (SIGINT)kill -2DOES cause the program to gracefully exit and run the shutdown hooks.I tried the following test program on OSX 10.6.3 and on
kill -9it did NOT run the shutdown hook, as expected. On akill -15it DOES run the shutdown hook every time.There isn’t any way to really gracefully handle a
kill -9in any program.The only real option to handle a
kill -9is to have another watcher program watch for your main program to go away or use a wrapper script. You could do with this with a shell script that polled thepscommand looking for your program in the list and act accordingly when it disappeared.