0

My LAMP installation on Ubuntu 18.04 LTS is working fine. But sometimes when MySQL is down it doesn't gets restarted automatically. So i google around and found below the bash script which checks whether mysql is running and will start the service if it is not running. Problem is that when mysql is down i receive the email but mysql is not started. Also, the user running the cron job has sudo access. My cronjob is: */5 * * * * sudo bash check_mysql.sh > /dev/null 2>&1

The bash script i used:

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

Is there anything wrong with the script or the cronjob is wrong?

2
  • what is the output of systemctl status mysql.service (or mysql) ? – Manula Waidyanatha Nov 19 '18 at 4:59
  • You don’t need sudo in a script for the system’s cron, it is run by root. – dessert Nov 19 '18 at 7:02
1

After digging more on google, i change the command from:

sudo /etc/init.d/mysql start

to

sudo service mysql start

and now it works.

2
  • 1
    Can you please mark your answer as correct so that askubuntu closes the question – akxer Nov 19 '18 at 5:34
  • @akabhirav since i am new to the community i can do it only after 2 days. – JohnC-- Nov 19 '18 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.