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

Posted by Straw Hat on Ask Ubuntu See other posts from Ask Ubuntu or by Straw Hat
Published on 2014-04-21T05:53:23Z Indexed on 2014/08/22 16:36 UTC
Read the original article Hit count: 556

Filed under:
|
|
|

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.

© Ask Ubuntu or respective owner

Related posts about bash

Related posts about mysql