15

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.

4 Answers4

20

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.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
3

Radu's answer nearly worked. I had to set the path to make it work:

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

Restart

With systemd you can set up your service to Restart on certain conditions:

systemctl cat mysql.service | grep Restart     # Check current status. cat PATTERN... Show files and drop-ins of specified units

Probably you wish to move from on-abort to on-failure, on [Service] section.

sudo EDITOR=nano systemctl edit mysql.service

Other useful commands:

sudo systemctl daemon-reload  # Reload systemd manager configuration. `systemctl edit` automatically does this for you.
systemctl cat mysql.service   # unit configuration
systemd-delta                 # Check the whole system changes

OOMScoreAdjust

On the same place, you may also want to adjust systemd OOMScoreAdjust:

Sets the adjustment value for the Linux kernel's Out-Of-Memory (OOM) killer score for executed processes. Takes an integer between -1000 (to disable OOM killing of processes of this unit) and 1000 (to make killing of processes of this unit under memory pressure very likely).

# Useful options not previously available in [mysqld_safe]

Kernels like killing mysqld when out of memory because its big.

Lets temper that preference a little.

OOMScoreAdjust=-600

Read also this excellent answer.


You can also reduce the memory usage of MySQL/MariaDB by tuning some options like max_connections, innodb_buffer_pool_size and innodb_buffer_pool_instances (probably on /etc/mysql/mariadb.conf.d/50-server.cnf).

Pablo Bianchi
  • 17,371
2

Radu's answer works - but this script works as well

#!/bin/bash
if [[ $(pgrep mysql | wc -l) = 0 ]];
then
    sudo service mysql start;
fi
JxAxMxIxN
  • 171