Question

Automatically restart MySQL on Ubuntu 16.04.x - If statement may be wrong?

Hey guys!

I have a Ubuntu 16.04.x server, where I installed MySQL. Works great and everything, but I would like MySQL to be restarted if it, for any reason stops. First of all, i know this isn’t a very good solution, because there is a reason that the MySQL proccess stops. But I also have a Ubuntu 14.04.x server which has been running for over a year, where it happend 2 times. In these cases it would be really nice to get it up and running again. I have my logs to see why it went down.

So to the actual thing I need help with:

I found this script, that claims to be able to restart MySQL if it stops:

I have made a file called mysqlfix.sh in /root/

#!/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin
if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]
then
    echo " The MySQL service on the server has been stopped. It has now been restarted." | mail -s " MySQL has been restarted" my@email.com
    sudo service mysql start
fi

Then made sure that it can run:

chmod +x mysqlfix.sh

Then added a cron job(crontab -e) that checks it every minute:

chmod +x mysqlfix.sh

This seems to be working on Ubuntu 14.04.x. But not on my new Ubuntu 16.04 server. It sends me an E-mail every minute with the “The MySQL has been restarted” message. This makes me think that the if statement is wrong. But I haven’t been able to find what is wrong with it.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Reason could be that Ubuntu 16.04.x is now using systemd instead of Upstart. This results in changed output of service status command. When you run /usr/sbin/service mysql status in terminal you will notice this:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2016-09-16 17:46:40 CEST; 1min 30s ago
  Process: 25747 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS)
  Process: 25744 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 25746 (mysqld)
    Tasks: 28
   Memory: 128.4M
      CPU: 265ms
   CGroup: /system.slice/mysql.service
           └─25746 /usr/sbin/mysqld

As far as you see, output is changed. Most important change is this: Active: active (running).

So, if you change your if to: if [[ ! "$(/usr/sbin/service mysql status)" =~ "active (running)" ]], your script should be working fine.

Whole script for reference will be:

#!/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin
if [[ ! "$(/usr/sbin/service mysql status)" =~ "active (running)" ]]
then
    echo " The MySQL service on the server has been stopped. It has now been restarted." | mail -s " MySQL has been restarted" my@email.com
    sudo service mysql start
fi

I have a similar script, but it can check other services as well. I recently updated it to work with 16.04:

https://github.com/sierracircle/services-checker

alexdo
Site Moderator
Site Moderator badge
May 30, 2022

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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel