There are moments my VPS works normally, and then there are times it crashes for no obvious reason. One insignificant bug or memory fault and your VPS service is down. Have you ever run into that panicking moment when your site suddenly goes down? I have many times, and for that reason, I began using watchdog timers to instantly restart any service that crashed. After using watchdog timers, I genuinely thought to myself, “Why didn’t I use this sooner?”
A watchdog timer checks your service again and again. If the service is unresponsive, it will restart the service automatically. It is easy to see why this is so effective—watch vigilantly and restart the service as soon as it fails.
Here's how they function:
Here is what systemd watchdogs provide:
With respect to why the watch dog timer is better:
Why Watchdog Timers Are So Useful
I think of watchdog timers as a trusted friend saying, “Hey, your service crashed so I restarted it.” Isn’t that a comforting thought?A watchdog timer checks your service again and again. If the service is unresponsive, it will restart the service automatically. It is easy to see why this is so effective—watch vigilantly and restart the service as soon as it fails.
How Watchdog Timers Interact with a VPS
I use watchdog timers for services that are known to fail easily, like Redis or Nginx, and even for a few of my self-built apps.Here's how they function:
- They monitor the service at preset time intervals.
- If the service does not respond, they consider it to have stopped working.
- They terminate the non-functioning process and restart it.
- Your users will not know a thing went wrong at all.
Easily Setting Up a Watchdog Timer
I have used a number of different means, but systemd watchdogs are the easiest for me. They are built into Linux, so I can just use them without loading anything special.Here is what systemd watchdogs provide:
- Automatic restart of service if it crashes
- Timeout checks
- Flexible control of check intervals
- Clean logs to simplify debugging problems
Reasons Why Watchdogs Are Better Than Cron Jobs
A few people use cron jobs to verify a service is up and running, but from my perspective a cron job in this case as a watchdog timer, weak in comparison.With respect to why the watch dog timer is better:
- Reaction time is immediate versus by seconds or minutes
- Monitoring with real time heartbeat signals
- Monitor crashes more reliably
- In the event of an issue, will yield more accurate results over time with fewer false alarms