Home / guides   Print version

Monitoring processes

Monitoring processes is important, sometimes a process crashes, and in a production environment this is no good.
So you need something to monitor those processes and if it goes down or crashes, automatically restart it.

there are several ways to do it. You can use a program like monit, great tool. Or you can write little daemon yourself to monitor a specific process.

write monitoring daemon

MySQL is a process that can be memory intensive, if system runs out of RAM, it starts killing processes to free up memory.
So MYSQL might crash sometimes.

Add the flowing code to /etc/systemd/system/mysql-monitor.service:

[Unit]
Description=MySQL Monitor
After=network.target

[Service]
Type=simple
User=root
ExecStart=/bin/bash -c 'while true; do if ! systemctl is-active --quiet mysql; then systemctl restart mysql; fi; sleep 60; done'

[Install]
WantedBy=multi-user.target

This scripts checks each 60s if mysql is still running, if not it restarts it.

Now you still need to run your little daemon:

sudo systemctl enable mysql-monitor
sudo systemctl start mysql-monitor

 

TOP