The following is the GNU All-permissive License as recommended in https://www.gnu.org/licenses/license-recommendations.en.html

Copyright (C) 2024 Free Software Foundation sysadmin@fsf.org

Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty.

Contributions are welcome. See https://savannah.gnu.org/maintenance/fsf/.

Auto-restart services

For non-systemd-native services like apache2 or coturn, you can implement the systemd behavior of auto-restart services by adding a config override:

cat /etc/systemd/system/coturn.service.d/override.conf
[Service]
Restart=always
RestartSec=5s
PIDFile=/var/run/turnserver.pid
RemainAfterExit=no
Type=forking

You can add that with systemctl edit coturn.service or apply via ansible like in commit [master 454c0a8] Autorestart coturn on failure

For a normal service where systemd correctly detects that the service is failed when the process died, you only need to add:

[Service]
Restart=always
RestartSec=5s

Note, that to restart forever, RestartSec will need to be >=3s, because of the defaults listed in /etc/systemd/system.conf:

#DefaultStartLimitIntervalSec=10s
#DefaultStartLimitBurst=5

If you want to restart faster, you need to change those values, for example:

[Unit]
StartLimitIntervalSec=0

However, you may also want to consider setting StartLimitAction= (man systemd.unit) to reboot the system.