How to control the rate of automatic restarts of a runit service

runitserviceunix

I have this runit service with run and log/run scripts properly working.

As it happens, the service itself can crash for external reasons and might not be able to start for many minutes. The default way that runit handles this situation is by restarting the service every couple of seconds. How do I change this behaviour?

My last insight was to add a check script and do some magic there, but it seems much more complicated than it should be. Is there a better simpler way?

Best Answer

I'm not familiar with this facility, however, if it was my task to solve this problem, and a very short man page reading did not offer a simple knob to tune this behaviour, I'd do the following:

Either extend the existing service start script, or if that is cumbersome, insert a new start script into the chain (which in turn starts the original start script). Instead of starting the service right away, the new start script should check if the last start happened recently enough. This can be done by checking a signaling file created by the previous start. If the file does not exist, the script can go on and touch the file and start the service. If the file exists, the script should check if the file is old enough. If it is not old enough, it should wait (sleep) in a loop until the file gets old enough.

Something like this might work (waits at least 1 minute between restarts):

#!/bin/bash

SIGNALDIR=/tmp
SIGNALFILE=service.started

while /bin/true; do
        found=`find "${SIGNALDIR}" -maxdepth 1 -name "${SIGNALFILE}" -mmin -1 | wc -l`
        [ "${found}" -eq 0 ] && break
        echo "Waiting"
        sleep 10
done

touch "${SIGNALDIR}/${SIGNALFILE}"
original service start...