Reiniciar automáticamente un servicio MySQL bloqueado con Cron en Ubuntu

  • Share this:
Reiniciar automáticamente un servicio MySQL bloqueado con Cron en Ubuntu

MySQL may crash when it runs out of memory or for some other issue. In most cases, you can resolve this by simply restarting the MySQL service.

Also Read: Setup VueJS on Ubuntu

To see if MySQL is running or not, run:

sudo service mysql status

If MySQL is down, you can restart it with:

sudo service mysql restart

But, what if you’re away on holidays while your client’s website goes down? Surely there’s a way to automate this process In Ubuntu with the following easy steps.

Create a Cron script to Auto Restart MySQL

You can configure cron to automatically check the status of the MySQL server and restart it if it crashes. Of course, this is not a permanent fix for your leaky MySQL server, but it can buy you time until you are able to investigate further.

We first need to create a simple BASH script and store it in the home folder (or anywhere you want), and then instruct cron to run this BASH script once a minute.

You can put this script anywhere, but in this example, we will put it in the home folder.

Change directory to the home folder.

cd /home/

Create a new directory here called scripts.

sudo mkdir scripts

Change to this directory.

cd scripts

Create a new file in this directory called using the nano text editor.

sudo nano

Paste in the following script.


# 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
    echo -e "MySQL Service was down. Restarting now...\n"
    sudo service mysql restart
    echo -e "MySQL Service is running already. Nothing to do here.\n"

Save and exit.

Make the script executable.

Also Read: How database effects the website Design

sudo chmod +x

Test the Script to Auto Restart MySQL

Test the script by running:

Also Read: Setup Ubuntu Server for Laravel

sudo ./

If MySQL is up and running, you should see:

MySQL Service is running already. Nothing to do here.

If you want to see what happens when the script detects if MySQL is down, stop the MySQL service, but only do this if your web server isn’t live! If your server is live but not busy, stopping and starting the MySQL server should only take a few seconds.

sudo service mysql stop

Now test the script again by running:

sudo ./


MySQL Service was down. Restarting now..

The MySQL service should be up again

sudo service mysql status

Add the MySQL Auto Restart Script to Crontab

By adding this script to crontab, the server will check the MySQL service once a minute, and if it isn’t running, it will restart it.

Open crontab (if asked to select a text editor, choose nano).

sudo crontab -e

In crontab, add the following line to the bottom of the file.


* * * * * /home/scripts/ > /dev/null 2>&1

Save and exit (press CTRL + X, press Y and then press ENTER)

Testing Crontab

To test if crontab is running the script once a minute, you will need to stop the MySQL service temporarily.

sudo service mysql stop

You can now test if MySQL is running with the following command.

sudo service mysql status

If MySQL is down, wait for at least a minute until the crontab runs. If after two minutes the MySQL service is still down, something has gone wrong with your cron script. Start MySQL again with sudo service mysql start.

Also Read: Install PHPStorm in Ubuntu

TWT Staff

TWT Staff

Writes about Programming, tech news, discuss programming topics for web developers (and Web designers), and talks about SEO tools and techniques