Linux C catching kill signal for graceful termination Linux C catching kill signal for graceful termination linux linux

Linux C catching kill signal for graceful termination


Catching signals is hard. You have to be careful. Your first step is to use sigaction to install a signal handler for the desired signals.

  • Choose a set of signals to respond to and choose what they mean for your process. For example, SIGTERM quits, SIGHUP restarts, SIGUSR1 reloads configuration, etc.

  • Don't try to respond to all signals and don't try to "clean up" after signal that indicates an error in your program. SIGKILL can't be caught. SIGSEGV, SIGBUS, and others like them shouldn't be caught unless you have a VERY good reason. If you want to debug, then raise the ulimit for core dumps — attaching a debugger to a core image is far more effective than anything you or I could ever code up. (If you do try to clean up after a SIGSEGV or something like that, realize that the cleanup code might cause an additional SIGSEGV and things could get bad quickly. Just avoid the whole mess and let SIGSEGV terminate your program.)

  • How you handle the signals is tricky. If your application has a main loop (e.g., select or poll) then the signal handler can just set a flag or write a byte to a special pipe to signal the main loop to quit. You can also use siglongjmp to jump out of a signal handler, but this is VERY difficult to get right and usually not what you want.

It's hard to recommend something without knowing how your application is structured and what it does.

Also remember that the signal handler itself should do almost nothing. Many functions are not safe to call from signal handlers.


You install signal handlers to catch signals -- however in 99% of cases you just want to exit and let the Linux OS take care of the cleanup -- it will happily close all files, sockets, free memory and shutdown threads.

So unless there is anything specifically you want to do, like sending a message on the sockets, then you should just exit from the process and not try to catch the signal.


I sometimes like to get a backtrace on SIGSEGV, the catching part goes like:

#include <stdio.h>#include <stdlib.h>#include <signal.h>void sig_handler(int);int main() {    signal(SIGSEGV, sig_handler);    int *p = NULL;    return *p;}void sig_handler(int sig) {    switch (sig) {    case SIGSEGV:        fprintf(stderr, "give out a backtrace or something...\n");        abort();    default:        fprintf(stderr, "wasn't expecting that!\n");        abort();    }}

You do want to be very careful handling these things, e.g. make sure you can't trigger another signal.