2

My application needs to do something before exit, I have already handled the ctrl-c case by handling signal of SIGINT, but I'd also like to handle the case when the system is going to reboot.

I researched a bit but found that SIGTERM is by administrative operation and system will not send SIGTERM before reboot, is it correct?

Any other signal that I can handle?

Edit: If my application is run by systemd, does it make it more complicated or easier to handle?

Felix Xu
  • 133

2 Answers2

2

The signals sent are customizable and that is documented in man systemd.kill. The most interesting parts are:

       KillSignal=
           Specifies which signal to use when stopping a service. This
           controls the signal that is sent as first step of shutting down a
           unit (see above), and is usually followed by SIGKILL (see above
           and below). For a list of valid signals, see signal(7). Defaults
           to SIGTERM.
       Note that, right after sending the signal specified in this
       setting, systemd will always send SIGCONT, to ensure that even
       suspended tasks can be terminated cleanly.

   FinalKillSignal=
       Specifies which signal to send to remaining processes after a
       timeout if SendSIGKILL= is enabled. The signal configured here
       should be one that is not typically caught and processed by
       services (SIGTERM is not suitable). Developers can find it useful
       to use this to generate a coredump to troubleshoot why a service
       did not terminate upon receiving the initial SIGTERM signal. This
       can be achieved by configuring LimitCORE= and setting
       FinalKillSignal= to either SIGQUIT or SIGABRT. Defaults to
       SIGKILL.

So when you systemctl stop *.service, systemd will send a SIGTERM by default to the main process in the service. If the process doesn't exit within TimeoutStopSec (default 90s), then systemd will follow up with a SIGKILL.

When you are developing your application, you don't need to be too considerate about the init system. If you don't want to code a response to SIGTERM, and want to simply keep SIGINT, you can. You just need to ensure any systemd service files you ship contain KillSignal=SIGINT.

I have not read anything to suggest systemd skips KillSignal= upon a reboot. If you ever see "Waiting for ..." when shutting down, that's systemd waiting for things to respond to KillSignal.

Stewart
  • 13,677
0

To my knowledge, all init systems implement more or less the same behavior. Which is, they send SIGTERM and wait for some amount of time and if the process does not terminate gracefully, they send a SIGKILL. I believe systemd works the same way. I could be wrong tho. The best way to know for sure is to test it.

I would recommend reading the description of the various signals and what do they mean and make your application react to them accordingly.

If your application is supposed to run as a long-lasting process, you should consider creating a systemd unit for it. This way, you will be able to control exactly what systemd should do with it when the system is shutting down.