Posts

Post not yet marked as solved
3 Replies
3.1k Views
I have a launchd helper process which constantly runs in the background, and when the user shuts down the machine, I need the helper to perform some clean up tasks before exiting. I've added a signal handler for SIGTERM in my helper code, like this:signal(SIGTERM, signalHandler); void signalHandler(int signal) { terminationCleanup(); exit(signal); }launchd documentation says that daemons should be sent a SIGTERM before a SIGKILL (my helper's launchd plist has ExitTimeOut set to 10, and EnableTransactions to false).However, the helper process never gets the SIGTERM on shutdown. If I try to unload the helper manually using launchctl unload <plist>, the helper never gets the SIGTERM, and then 10 seconds later, the system logger shows: "Service did not exit 10 seconds after SIGTERM. Sending SIGKILL.".Even if I try to do it manually by sending sudo kill -15 pid, the SIGTERM signal handler is not triggered.At this point, you may think "well, obviously your signal handler isn't set up correctly"... well, I also am handling the SIGABRT signal, so what I found is that if I first do sudo kill -6 pid, my signal handler is triggered and I catch the SIGABRT properly. My helper launchd plist also has KeepAlive set to true, so the helper process will restart immediately after. Now, if I do kill -15 pid, or launchctl unload <plist>, now the SIGTERM is actually sent and caught by my helper.So, it makes no sense. Why does my helper process only handle the SIGTERM signal after the process has been sent SIGABRT, and a new process is spawned?
Posted Last updated
.