C programs that can receive shutdown/termination requests from linux/upstart/ubuntu

C programs that can receive shutdown/termination requests from linux/upstart/ubuntu … here is a solution to the problem.

C programs that can receive shutdown/termination requests from linux/upstart/ubuntu

I wrote a program for Linux in C. Currently I’m running Ubuntu Upstart as a background service.

I would like to be able to have the program close gracefully when it receives a command, rather than being killed. Can someone point to the function used to receive such commands?

(EDIT: can’t answer my own post, but it seems like I’m going to use the signal.h signaling function to put callbacks on SIGTERM and SIGKILL).

Solution

You want to use sigaction(2) to define a handler that runs when SIGTERM is captured.

Related Problems and Solutions