Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple bash script to check whether MySQL is running.
#!/bin/bash
UP=$(pgrep mysql | wc -l);
if [ "$UP" -ne 1 ];
then
echo "MySQL is down.";
sudo service mysql start
else
echo "All is well.";
fi
@mheadd

This comment has been minimized.

Owner

mheadd commented May 13, 2013

Run this script every 5 minutes using a cron job like this one:

 */5 * * * * /home/user/scripts/monitor.sh > /dev/null 2>&1
@dirkcuys

This comment has been minimized.

dirkcuys commented Oct 30, 2013

I had to set the PATH variable to include /sbin in the crontab for the script to be run successfully by cron.

PATH=/usr/sbin:/usr/bin:/sbin:/bin
@ottomatias

This comment has been minimized.

ottomatias commented Sep 18, 2014

I don't know which distro are you using but to me it seems a better version is:

UP=$(pgrep mysql | wc -l);
if ["$UP" -eq 0];
then
echo "MySQL is down.";
sudo service mysql start

else
echo "All is well.";
fi

That is even more logical because if it's not running it should not print out anything (-eq 0). If it is, it should print out something (is not equal to zero). Your script starts up mysql if there is more than one instance running.

@arthurmolina

This comment has been minimized.

arthurmolina commented Oct 14, 2014

Isn´t it better to check if it is running by using this?

UP=$(service mysql status|grep 'mysql start/running' | wc -l);
@sanchitrasiya

This comment has been minimized.

sanchitrasiya commented Dec 10, 2015

Unfortunately that haven't worked for me. This worked well:
File /etc/cron.hourly/sc.sh:

#!/bin/bash
/etc/init.d/mysqld status | grep "stopped\|dead"
if [ $? -eq 0 ]; then
        echo "`date` - MySQL not running" >> /tmp/checkstatus
fi

And to check this every minute, I have added a cron job as:
* * * * * sh /etc/cron.hourly/sc.sh

Hope that helps someone!
-Sanchit

@savalo

This comment has been minimized.

savalo commented Mar 2, 2016

#!/bin/bash
UP=$(/etc/init.d/mysql status | grep running | grep -v not | wc -l);
if [ "$UP" -ne 1 ];
then
        echo "MySQL is down.";
        service mysql start

else
        echo "All is well.";
fi
@andronick83

This comment has been minimized.

andronick83 commented Mar 27, 2016

Worked for me in Debian
* * * * * /usr/sbin/service mysql status || /usr/sbin/service mysql start

@vtatntpt

This comment has been minimized.

vtatntpt commented May 17, 2017

awesome!

@richardwhitehead

This comment has been minimized.

richardwhitehead commented May 25, 2017

The original script does not work on my system. MySql runs 2 processes, so UP gets set to 2 and it thinks the service is not running.
Simply checking the error code from service status works.
This is on Debian.

@madurapa

This comment has been minimized.

madurapa commented Dec 21, 2017

If you are using Redhat (RHEL)/Fedora/CentOS

#!/bin/bash
/etc/init.d/mysqld status | grep 'running' > /dev/null 2>&1
if [ $? != 0 ]
then
sudo /etc/init.d/mysqld restart
fi

Don't forget to give the file executable permission sudo chmod +x monitor.sh

@lonelycowboy

This comment has been minimized.

lonelycowboy commented Nov 19, 2018

For me, i used this:

#!/bin/bash
email='email@example.com'
subject='mysql process down and up'
MYSQL_START='sudo service mysql start'
MYSQL='mysqld'
PGREP='/usr/bin/pgrep'
#check pid
$PGREP $MYSQL
if [ $? -ne 0 ]; then
$MYSQL_START | mail -s "$subject" $email <<< 'MySQL service was down and successfully started'
fi

and then
*/5 * * * * /home/user/scripts/mysql_check.sh > /dev/null 2>&1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment