Monday , October 23 2017 12:28 AM
Home / Tutorials / Linux / How-To: Create a Cron Job to start MySQL if it Stops

How-To: Create a Cron Job to start MySQL if it Stops

Sometimes server act weird and stop some services due to any issue. I faced this issue with my  sites , where MySQL service stops after running the clean script to remove demo demo sites after three days. So I created a Cron Job on my AWS Ubuntu Instance to check if and start MySQL service if its 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. AWS provides ssh access as soon as you create a instance service. here am using nano editor you can use any editor instel of nano (Ex. vim, gEdit, gvim etc)

Creating Shell Script

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

Step 2: Create shell script file.

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

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 terminal and mysqlfix.sh file will be created in the root directory.

Step 5: Give this file executable permissions

Testing the Script

Now our script is ready, lets test if this runs fine.

Enter following commands in terminal:

Terminal will print something like this:
mysql start/running, process 58428

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

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, lets create the cron job to run our script every minute

Create Cron Job

Step 1: Installing the cron job

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

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

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

Now our cron job is installed.

To test if it runs fine, Type following command:

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

Hope you find this helpful.

Note: If you got this error install mailx/mailutils to enable AWS internal mail

Depending on the Linux distribution you’re using, use one of the following command to install the mailx.

Red Hat / CentOS / Scientific Linux

Debian / Ubuntu

 

About GOPU

Technology Enthusiast with a keen eye on the Cyber-security and other tech related developments.

Powered by themekiller.com anime4online.com animextoon.com apk4phone.com tengag.com moviekillers.com