Background
I've been asked to create a systemd
script for a new service, foo_daemon
, that sometimes gets into a "bad state", and won't die via SIGTERM
(likely due to custom signal handler). This is problematic for developers, as they are instructed to start/stop/restart the service via:
systemctl start foo_daemon.service
systemctl stop foo_daemon.service
systemctl restart foo_daemon.service
Problem
Sometimes, due to foo_daemon
getting into a bad state, we have to forcibly kill it via:
systemctl kill -s KILL foo_daemon.service
Question
How can I setup my systemd
script for foo_daemon
so that, whenever a user attempts to stop/restart the service, systemd
will:
- Attempt a graceful shutdown of
foo_daemon
viaSIGTERM
. - Give up to 2 seconds for shutdown/termination of
foo_daemon
to complete. - Attempt a forced shutdown of
foo_daemon
viaSIGKILL
if the process is still alive (so we don't have a risk of the PID being recycled andsystemd
issuesSIGKILL
against the wrong PID). The device we're testing spawns/forks numerous processes rapidly, so there is a rare but very real concern about PID recycling causing a problem. - If, in practise, I'm just being paranoid about PID recycling, I'm OK with the script just issuing
SIGKILL
against the process' PID without being concerned about killing a recycled PID.