In what order should I send signals to gracefully shutdown processes?

SIGTERM tells an application to terminate. The other signals tell the application other things which are unrelated to shutdown but may sometimes have the same result. Don’t use those. If you want an application to shut down, tell it to. Don’t give it misleading signals.

Some people believe the smart standard way of terminating a process is by sending it a slew of signals, such as HUP, INT, TERM and finally KILL. This is ridiculous. The right signal for termination is SIGTERM and if SIGTERM doesn’t terminate the process instantly, as you might prefer, it’s because the application has chosen to handle the signal. Which means it has a very good reason to not terminate immediately: It’s got cleanup work to do. If you interrupt that cleanup work with other signals, there’s no telling what data from memory it hasn’t yet saved to disk, what client applications are left hanging or whether you’re interrupting it “mid-sentence” which is effectively data corruption.

For more information on what the real meaning of the signals is, see sigaction(2). Don’t confuse “Default Action” with “Description”, they are not the same thing.

SIGINT is used to signal an interactive “keyboard interrupt” of the process. Some programs may handle the situation in a special way for the purpose of terminal users.

SIGHUP is used to signal that the terminal has disappeared and is no longer looking at the process. That is all. Some processes choose to shut down in response, generally because their operation makes no sense without a terminal, some choose to do other things such as recheck configuration files.

SIGKILL is used to forcefully remove the process from the kernel. It is special in the sense that it’s not actually a signal to the process but rather gets interpreted by the kernel directly.

Don’t send SIGKILL.SIGKILL should certainly never be sent by scripts. If the application handles the SIGTERM, it can take it a second to cleanup, it can take a minute, it can take an hour. Depending on what the application has to get done before it’s ready to end. Any logic that “assumes” an application’s cleanup sequence has taken long enough and needs to be shortcut or SIGKILLed after X seconds is just plain wrong.

The only reason why an application would need a SIGKILL to terminate, is if something bugged out during its cleanup sequence. In which case you can open a terminal and SIGKILL it manually. Aside from that, the only one other reason why you’d SIGKILL something is because you WANT to prevent it from cleaning itself up.

Even though half the world blindly sends SIGKILL after 5 seconds it’s still horribly wrong thing to do.

Leave a Comment