Question

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

  • Posted on February 9, 2015
  • minhbkxAsked by minhbkx

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 :-)

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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