Shell script issue: cron job script to Restart MySQL server when it stops accidentally

Straw Hat

I have this script, I am using it to setup CRON job to execute this script, so it can check if MySQL service is running; if not then it restart the MySQL service:

#!/bin/bash
service mysql status| grep 'mysql start/running' > /dev/null 2>&1
if [ $? != 0 ]
then
    sudo service mysql restart
fi

I have setup cron job as.

sudo crontab -e

and then added,

*/1 * * * * /home/ubuntu/mysql-check.sh

Problem is that it restart MySQL on every cron job execution.. even if server is running it restart the MySQL service what is correction in the script to do that.

Radu Rădeanu

I suspect that you setup the cron job to execute this script in your crontab file, and not in the root crontab file. This is not correct because if you don't run service mysql status as root, the mysql service will not be recognized.

So, modify the script as follow:

#!/bin/bash
if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]
then
    /usr/sbin/service mysql start
fi

Be sure that is executable:

chmod +x /path/to/script

Then add a new entry in the root crontab as follow:

  • Edit root crontab file using:

    sudo crontab -e
    
  • And add the following line to the file:

    */1 * * * * /path/to/script
    
  • Note: I have set the cron job for every minute, but you can change as you wish or as you think is better. See http://en.wikipedia.org/wiki/Cron in this sense.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute PHP script in cron job

From Dev

Shell Script Random Cron job

From Dev

Python script elicits error when run in cron job but at no other time

From Dev

Shell script with a cron job to start a program if not running?

From Dev

I have errors in a script, but only when it runs as a cron job

From Dev

How to setup cron job for a codeigniter script on server

From Dev

PHP script does not run as cron job on Ubuntu Server

From Dev

Cron job script not working

From Dev

How to set a cron job to run a shell script?

From Dev

Cron job doesn't read into the shell script function

From Dev

Basic Cron job to run a shell script to append date in a log, not working

From Dev

Shell script issue: cron job script to Restart MySQL server when it stops accidentally

From Dev

How to set a cron job to run a shell script?

From Dev

shell script for cron job

From Dev

Shell script truncated when run from cron or at

From Dev

Shell script is not working via cron job

From Dev

Run Rails script in cron job?

From Dev

How do I restart a python script on a remote server if it stops?

From Dev

Shell script runs manually but not executed through cron job

From Dev

Shell script with a cron job to start a program if not running?

From Dev

Hostgator cron job not working with this script

From Dev

Shell script issue when added to cron

From Dev

Cron not working with shell script

From Dev

How to mail output of shell script as a cron job

From Dev

Cron Job: Redirecting shell script output to a file

From Dev

shell script in cron not working

From Dev

Cron job script not working

From Dev

Cron job doesn't read into the shell script function

From Dev

Cron job for an R script failing

Related Related

HotTag

Archive