Monit is a tool for monitoring and restarting any services.
Installation on Ubuntu/Debian:
sudo apt install monit
I will give an example of the parameters of the configuration file /etc/monit/monitrc:
# Running monit in the background and checking processes at intervals in seconds
set daemon 120
# Path to the log file
set logfile /var/log/monit.log
# Mail server address for message delivery
set mailserver localhost
# Alert storage location and their limit
set eventqueue
basedir /var/lib/monit/events
slots 100
# Email format
set mail-format { from: monit@example.com }
# Alert Address
set alert admin@example.com
# System Resource Status
check system myhost.mydomain.tld
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if swap usage > 25% then alert
if cpu usage (user) > 70% then alert
if cpu usage (system) > 30% then alert
if cpu usage (wait) > 20% then alert
Startup options are in the /etc/default/monit file.
The service monitoring parameters are in the /etc/monit/monitrc.d directory, you can attach them at the end of the main configuration file /etc/monit/monitrc, for example like this:
include /etc/monit/monitrc.d/apache2
Official website: mmonit.com
See also my article:
Simple Watchdog script