Hey guys, I just whipped up a little component to allow running HA as a proper systemd daemon, with support for the watchdog!
Now, why would anyone want such a thing? Well, this blog post by one of the systemd developers explains how the systemd watchdog works better than I can, so I won’t go into a lot of detail here. Basically, it’s a way for systemd (which is the backbone of most modern Linux systems) to make sure that HA is actually alive and working.
Let’s say, for example, HA’s core loop locks up somehow, but doesn’t actually crash. In that case, our little component would stop “petting” the watchdog every 15 seconds, in which case systemd would automatically kill and restart it. You can optionally setup systemd to automatically reboot the system if a service fails more than X times in a Y minute interval!
When you combine this with a hardware watchdog, which embedded systems like the Raspberry Pi have onboard, you end up with a top down method that ensures your critical services stay operational. (So, the hardware watchdog makes sure systemd is functioning and systemd makes sure your services are functioning.)
In addition to systemd watchdog support, I’ve also added systemd notify hooks. In a nutshell this allows HA to tell systemd when startup has completed, when it’s shutting down and reports the PID of the main process.
This is important for a number of reasons, but the main one is this: HA can take awhile to fully startup, sometimes on the order of minutes! If a process has no way to talk to the init system, the init system has no way to know if the process has actually finished startup or if it’s hung. Now that we can tell systemd when we’ve finished starting, we can use another cool systemd feature: Startup time limits. Which is exactly what it sounds like; basically, if a process doesn’t report a READY status in X seconds, systemd will consider it failed and restart it. There is also a shutdown time limit as well, in which case systemd can hard kill the process.
So, if you want to try out these awesome sounding features, you can grab my component here.
I’ve been running it for a few days and it seems rock solid, but obviously more testers would be appreciated!
Let me know if the instructions aren’t clear enough, but it should be rather simple to setup. I’ve also provided a sample systemd service file for Home Assistant to get you going.
This is my first true HA component, so feedback on everything from the quality of the code, to bugs, to better ways I could be doing things is welcomed!