Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem where MySQL (running on Ubuntu 12.04 with all latest updates) dies after a few days so put this script into a cron job that runs as root every 60 seconds.

Everything works except the mysql service won't restart, I've tried using "service mysql restart" and "/usr/bin/mysqld" and they don't work either.

I am using the full path to restart command so I don't believe that's an issue. Right now the only thing that works is replacing the restart command with a "reboot" and that is very ugly and I want to avoid it.

#Check if MySQL is up, if not then start it
# mysql root/admin username
# mysql admin/root password
# mysql server hostname
#Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
# Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS.
MSTART="/etc/init.d/mysql restart"
# Email ID to send notification
# path mysqladmin
MADMIN="$(which mysqladmin)"


# see if MySQL server is alive or not

$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
        echo "" >$MAILMESSAGE
        echo "ALERT: MySQL Server is not responding to ping">>$MAILMESSAGE
        echo "Hostname: $(hostname)" >>$MAILMESSAGE
        echo "System Time: $(date)" >>$MAILMESSAGE
        # try to start mysql
        # see if it is started or not
        o=$(ps cax | grep -c ' mysqld$')
        if [ $o -eq 1 ]; then
                sMess="MySQL server successfully restarted"
                sMess="MySQL server FAILED to restart"
        # Email status too
        echo "Current Status: $sMess" >>$MAILMESSAGE
        echo "" >>$MAILMESSAGE
        echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
        echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
        # send email
        sendemail -o message-content-type=text -f -t $EMAILID -u MySQL GURU ALARM -m < $MAILMESSAGE
else # MySQL is running :) and do nothing
# remove file
share|improve this question
Have you looked into why it dies in the first place? – Nick Jun 25 '12 at 0:25
Probably something to do with lots of databases and lack of RAM - trying to convince the owner to upgrade, meanwhile this is my only recourse. – Tyler Jun 25 '12 at 1:13
up vote 4 down vote accepted

Without seeing the startup error log, it would be near impossible to diagnose.

If I had to hazard a guess, it sounds likely to be ulimit at fault, the process is probably unable to allocate the memory it needs from a cron, but isn't limited when run via interactive shell. The MySQL log would confirm this, albeit in a non-descript fashion.

Also, don't re-invent the wheel with half cocked scripts, just use a recognized monitoring application like monit

share|improve this answer
Monit solved the problem, thank you sonassi – Tyler Jun 25 '12 at 1:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.