Created
May 13, 2013 20:00
-
-
Save mheadd/5571023 to your computer and use it in GitHub Desktop.
Simple bash script to check whether MySQL is running.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Start mysq if not started
UP=$(pgrep mysql | wc -l);
if [ "$UP" -le 1 ];
then
echo "Iniciando mysql...";
sudo service mysql start
else
echo "Mysql online.";
fi
One line cron to check and start apache2
and mariadb
services if they're not running (you can change mariadb
to mysql
):
* * * * * if [ $(pgrep apache2 | wc -l) -le 0 ]; then service apache2 start; fi
* * * * * if [ $(pgrep mariadb | wc -l) -le 0 ]; then service mariadb start; fi
Better Script:
#!/bin/bash
email='[email protected]'
subject='MySQL process down and restarted'
MYSQL_START_COMMAND='sudo service mysql start'
MYSQL_PROCESS_NAME='mysqld'
PGREP_COMMAND='/usr/bin/pgrep'
#Check if MySQL is running
$PGREP_COMMAND $MYSQL_PROCESS_NAME
#If MySQL is not running, start it and send an email
if [ $? -ne 0 ]; then
$MYSQL_START_COMMAND
echo 'MySQL service was down and has been successfully started.' | mail -s "$subject" $email
fi
*/5 * * * * /root/mysqlstatus.sh > /dev/null 2>&1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For me, i used this:
and then
*/5 * * * * /home/user/scripts/mysql_check.sh > /dev/null 2>&1