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.servicesystemctl stop foo_daemon.servicesystemctl 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_daemonviaSIGTERM. - Give up to 2 seconds for shutdown/termination of
foo_daemonto complete. - Attempt a forced shutdown of
foo_daemonviaSIGKILLif the process is still alive (so we don't have a risk of the PID being recycled andsystemdissuesSIGKILLagainst 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
SIGKILLagainst the process' PID without being concerned about killing a recycled PID.