Question

cron job to auto restart mysql or httpd or even droplet when errors

  • Posted February 9, 2015

Hi all,

Sometimes my droplet has a problem Error establishing database connection. Im fixing it now. The temp way is to restart mysql then things would be ok for few hours or days.

Im also thinking a temporary way until i can truly solve the above problem. Step A. Auto restart mysql whenever error happens => I created a cron job to do this. Step B. But sometimes even the mysql or http were hanging out and cant be restarted. In this time I had to poweroff my droplet then turn it on again.

So how to define the way for this cron job? I knew how to do A but not B. Mine: Wordpress, Centos 6.5 64, Virtualmin

Thank you, Minh PS: Im not a tech guy so pls dont use complex jargons :-)

Subscribe
Share

Can you write how to do A ?


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello there,

What can be done here is to optimise the MySQL performance and inspect the Apache logs (error_log) to check if the Apache workers (MaxRequestWorkers) have been exceeded as well.

For Apache, I’ll recommend using mod mpm_event or mpm_worker.

https://www.digitalocean.com/community/tutorials/how-to-configure-apache-http-with-mpm-event-and-php-fpm-on-ubuntu-18-04

You can check this question which was asked in our community on how to optimise the MySQL performance:

https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

Regards

thanks EpicCDN for your detailed answer :-)

if you have a cron job to restart apache and mysql sometimes the service can’t be correctly stopped, this is some kind of issue with users, so you need to kill the process, you can try to read pid file for process id and kill -9 that id, then restart.

Again as suggested, fix MySQL problem or this will solve nothing, only cause you a db corruption.

here some samples for restarts, you can try to add a counter of tries, if you restart X service more than 3 times in a row then #reboot:

#!/bin/bash

#Memcached if service memcached status | grep -q “running”; then echo “MemCached is up” else service memcached restart cat /root/alert.txt | mail -s “MemCached Service Down” myemail@gmail.com fi

#This if is just a sample of reboot, you need to use correct syntax here

#--------------------------------------------------------------------- if cat /root/count.txt = 111; then reboot fi #Sample end

#Nginx + counter if service nginx status | grep -q “running”; then echo “Nginx is up” echo 0 > /root/count.txt else service nginx restart cat /root/alert.txt | mail -s “Nginx Service Down” myemail@gmail.com echo 1 >> /root/count.txt fi

#---------------------------------------------------------------------

#PHP-FPM if service php-fpm status | grep -q “running”; then echo “PHP-FPM is up” else service php-fpm restart cat /root/alert.txt | mail -s “PHP-FPM Service Down” myemail@gmail.com fi

#PHP-FPM Max Children Status if curl -s http://site.com/status | grep -q “max children reached: 0”; then echo PHP running normal; else service php-fpm reload cat /root/alert.txt | mail -s “PHP-FPM Max Children reached!” myemail@gmail.com fi

The most common reason for MySQL to crash like this is that the process is running out of memory. Rather than treat the symptom by restarting the process (this will eventually lead to data corruption due to the crashes) we can treat the cause. There are a couple things you can do.

1.) Set up a swap file on your droplet. This file will be used when your physical memory is exhausted, swapping data out here rather than allowing services to crash.

2.) Optimize the processes running on your droplet. A number of tutorials on optimizing various apps can be found here.

3.) Finally, you can choose to upgrade to a droplet plan which provides more RAM.

A useful command while doing this is

free -m

This will display a summary of your used and available memory and help you to track how your changes and optimizations are affecting resource use.