Get 25% off on any of our premium products till Sept 30th, 2017. Offer code: cjaddons

Sometimes server acts weird and stops some services due to any issue. I faced this issue with my CSSJockey.net WordPress demo sites server, where MySQL service stops after running the clean script to remove demo sites after three days. So I created a Cron Job on my DigitalOcean Ubuntu Droplet to check if and start MySQL service if it’s not running. I scheduled this to run the check every minute. Here’s the code and steps to setup this cron job.

NOTE: You must have ssh access to your server. DigitalOcean provides ssh access as soon as you create a droplet.

Creating Shell Script

Step 1: Open Terminal and login to your server as root via ssh.

Step 2: Create shell script file.

cd ~
nano mysqlfix.sh

This will let you create a shell script in the root directory for the root user. You can use any name as per your preferences for the .sh file.

Step 3: Write the script to check and restart MySQL and send an email alert.

#!/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin
if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]
then
    echo "MySQL restarted" | mail -s "Email Subject" email@domain.com
    sudo service mysql start
fi

Make sure you change the Email Subject and email address.

Step 4: Once you are done with the script press CTRL + x and you will be asked to save the changes. Type Y and hit enter. You will be returned to the terminal and mysqlfix.sh file will be created in the root directory.

Step 5: Give this file executable permissions

chmod +x mysqlfix.sh

Testing the Script

Now our script is ready, let’s test if this runs fine.

Enter following commands in the terminal:

/usr/sbin/service mysql status

The terminal will print something like this:
mysql start/running, process 2409

/usr/sbin/service mysql stop

This will stop MySQL service, to verify run the status command again and it will print something like this:
mysql stop/waiting

~/mysqlfix.sh

This will run the script we created to check and restart the service. You should get an email with the subject and message to the email address you specified in the script.

Run the status command again and it should print
mysql start/running, process ####

If everything goes well in this step, let’s create the cron job to run our script every minute

Create Cron Job

Step 1: Installing the cron job

Make sure you are logged into your server via ssh as root, then type the following command in terminal:

crontab -e

Once you see the crontab screen type the following line at the end of this file:

*/1 * * * * /root/mysqlfix.sh

Press CTRL + x and you will be asked to save the changes, press Y and hit enter. You will see the following line in terminal:
crontab: installing new crontab

Now our cron job is installed.

To test if it runs fine, Type following command:

/usr/sbin/service mysql stop

This will stop MySQL service on your server. Now you should wait for one minute and get an email with the subject and message we specified in the mysqlfix.sh file.

Hope you find this helpful.

 

Mohit Aneja

  • Jaris

    Step 5 is wrong
    chmod +x launch.sh

    it should be chmod +x mysqlfix.sh

  • Jamesy

    Thanks, man. Just had to add ‘bash’ before the path to the mysqlfix.sh file to have it executed in the crontab file.
    Also worth noting that your filename in step 5 is wrong – should be mysqlfix.sh.

    Many thanks,
    James

  • Stay up-to-date!
    Join our mailing list to receive product updates & announcements. Every month we give away 10 FREE Licenses to our lucky subscribers.

    No Spam Ever!

Stay up-to-date!

Get notified of product updates, new products and freebies.