Question

Automatically reboot server

Posted June 10, 2014 3.3k views
I was wondering if there Is away to automatically reboot server if the database went unresponsive?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers
Restart the whole machine? Or just the MySQL instance? I'm thinking it's the latter, in which case you can add a cronjob at your own desired interval which will periodically check if MySQL is running and start it if it is not.
/etc/init.d/mysql status || /etc/init.d/mysql start

Hello, @cameron1

You can create a simple bash script to check if MySQL is running and if not to restart it.

#!/bin/bash

# Check if MySQL is running
sudo service mysql status > /dev/null 2>&1

# Restart the MySQL service if it's not running.
if [ $? != 0 ]; then
    sudo service mysql restart
fi

Run this script every 5 minutes using a cron job like this one:

 */5 * * * * /home/user/scripts/monitor.sh > /dev/null 2>&1

Hope that this helps!
Regards,
Alex

Submit an Answer