Script Needed to Monitor MySQL and restart it when it is Down


Well-Known Member
Dec 1, 2010
cPanel Access Level
Root Administrator
Such a feature already exists within cPanel * WHM.

Documentation: Service Manager

Simply go to: WHM -> Service Manager

Then, make sure both "Enabled" and "Monitor" are checked for "mysql". This will instruct cPanel's chkservd mechanism (which runs onc per 5 minutes) to check on the status of MySQL and restart as necessary. If a restart is necessary, it will also email you about it if you have your email contact setup in WHM for the server.

However, if MySQL is crashing/unavailable that often that you're specifically asking for an auto-restart script, I would be more inclined to suggest you identify the cause of the outages. Applying a bandaid fix by auto-restarting MySQL is fine for the short-term, but you'll want to find out why MySQL is crashing in the first place so that you can prevent that from happening.

What does the MySQL log say during the time of the crash?

Feel free to open a ticket (see my signature for links) if you'd like assistance in attempting to identify the cause of MySQL crashing on your server.


Well-Known Member
Dec 12, 2011
Islamabad, Pakistan
cPanel Access Level
Root Administrator
Hi Brian,

Thanks for your assistance, but actually I need a script that I can run on the servers where there is no cpanel, I have few servers that are not running any cpanel so I need a written script so that I can monitor the mysql daemon over there with the help of that.

So any ideas or clues for it?


Well-Known Member
Aug 19, 2010
Gods Own Country
cPanel Access Level
Root Administrator

It is easy to achieve this through a small shell script; something like following perhaps :)


#Checking whether MySQL is alive or not

if mysqladmin ping | grep -q "alive"; then

echo "MySQL is up"


/etc/init.d/mysqld restart


Please note that above script will check whether MySQL is up or not. If you need to monitor anything else ( like total queries, ect ), you can enhance the above script :)

Additionall you can run it a deamon to check MySQL every second ( if you want ). For that you just need to put the above script in an infinite loop and give one second intervel ( sleep 1 ) for the above check. Please note, it may create an extra overhead ( but not much ) for checking mysql every second.

Thank you,


Quality Assurance Analyst
Staff member
Oct 2, 2010
somewhere over the rainbow
cPanel Access Level
Root Administrator
If this is a non-cPanel machine, you might consider using SIM at System Integrity Monitor | R-fx Networks location. This is the same company that provides APF firewall and PRM for resource monitoring. SIM will check services to see if they are online and restart those that are not.