FreeBSD – Keep a Daemon Running

freebsd

Please note this is a FreeBSD question and not a Linux question. Please don't answer with how it would be done on Linux or systemd or any of that.

I have a situation where memcached is crashing. It's not that repeatable and I'll eventually figure it out. In the meantime, I need to ensure that memcached is running. If it's not, I need to restart it. It is installed via pkg and starts via /usr/local/etc/rc.d/memcached. There are a few choices.

  1. I could write a watchdog script and invoke it every like 10 minutes or something via cron. Kinda ugly, but would work. Main thing here is that I need to go write that script. Calling service memcached status, evaluate the result, maybe call service memcached start. I know how to write that, but it seems clunky. I'd rather just use a mechanism that already exists.
  2. I could write a do ... until loop script. Then I could modify /usr/local/etc/rc.d/memcached. But I want to keep files that were installed by the package pristine. I don't want to perpetuate my changes each time I upgrade the package.
  3. I drop a script into /usr/local/etc/periodic.d/hourly and have it invoked by periodic(8).

Is there some easy, FreeBSD-native mechanism that I'm not thinking of to keep processes running? Or am I just overthinking it and I should just go write my 8 line script and start calling it from cron?

Best Answer

What you're looking for is called a supervisor. I don't think FreeBSD comes with one out of the box. But there are some in the ports. I see at least;

  • supervisord is available as a port called py-supervisor (the port has several flavors, install with pkg install py37-supervisor or whatever matches your Python version).
  • daemontools is available as a port.
  • Monit is available as a port.
  • FSCD is available as a port called fsc.

I suggest supervisord. Install the package and add a stanza to /usr/local/etc/supervisord.conf:

[program:memcached]
command=/usr/local/etc/rc.d/memcached

To run supervisord at boot time, edit /etc/rc.conf or /etc/rc.conf.local to have the line

supervisord_enable="YES"

Whichever supervisor you choose, make sure to disable the direct starting of memcached.

Related Question