A daemon writes a PID file and ops scripts use it, but after crashes the stale PID points to a different process. How do you fix the design?
AdvancedScenarioProcesses, services and systemd
Answer
Situation read → PID files are racy; let the supervisor track the process.
What I'd do →
- Run the daemon in the foreground under systemd (
Type=simple/notify), which tracks it by cgroup. - Scripts use
systemctl is-active app,systemctl kill apporsystemctl show -p MainPID app. - If PID files must stay, put them in
/run(cleared at boot) viaRuntimeDirectory=app.
Why → cgroup tracking can't confuse processes, and /run is a tmpfs, so stale files vanish on reboot.
Interview tip → "Let systemd own process lifecycle" is the senior answer.
More on Processes, services and systemd
Your team has a Python API that someone starts with `nohup python app.py &`. Make it a proper service running as a non-root user.BeginnerThe service you created must bind to port 443, but it runs as a non-root user. How?BeginnerYou need to change one setting (memory limit) in a vendor-supplied unit file without editing it. How?BeginnerA service depends on a database on the same host and fails at boot because the DB isn't ready. How do you fix the ordering?IntermediateA service keeps crashing and systemd stops restarting it after a few attempts. How do you tune this?IntermediateA long-running app must reload its config without dropping connections. How do you wire this into systemd?Intermediate