On Ubuntu how do I check if my mySQL server is up and running using CRON and restart it if it is not?

Hi all,

I have a ubuntu 14.04 x64 distribution on a Linode VPS with Apache 2.4.7 and mySQL server 5.5.

It doesn't happen everyday but sometimes, when there are a lot of users connected to my server, the mysql server hangs and doesn't restart on its own. I have to manually restart it using putty but since only I have ssh-based admin access, only I can do that. Basically, if MySQL were to hang with me out of office or sleeping, then all my sites would be offline till I got back and rebooted or restarted the MySQL service manually. I would like to use CRON to create a script that could do one of the following things:


To check every x minutes if MySQL server is up and running and if NOT so then restart it automatically.



To restart it everytime it crashes

Could anyone please assist? I have no experience with CRON or .sh scripts and I have only started using linux last month.

thanks in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
It crashes or it hangs?
There's a big difference, cause if it hangs then it's still opened, just not responding (you can verify with ps aux | grep mysqld), and you have to kill it first, then start it.
If it crashes (never happened to me, I would look for problems elsewhere) then you just need to start it.

FWIW, I think you're approaching this the wrong way. If your server hangs regularly then you have a problem either on server's specs (underpowered for your needs) or on the queries you're running. Find the cause and solve it.

Killing Mysql regularly will only help you corrupt your data, in the long run.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.