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

Posted on 2014-08-31
Last Modified: 2014-09-05
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
Question by:badwolfff
    1 Comment
    LVL 34

    Accepted Solution

    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.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
    Creating and Managing Databases with phpMyAdmin in cPanel.
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now