Bash Script To Restart mysql Ubuntu 14.04

Posted July 10, 2014 29.5k views


I need a Bash Script To Restart mysql, can someone help please ?

Thank you very much!

1 comment
  • Hey - I had this issue, but I only wanted to restart mysql if it had stopped.

    To simply restart MySQL you can add the following to the root CRONTAB:

    sudo crontab -e

    This restarts every minute:

    */1 * * * * run-one sudo service mysql restart

    If you want to check if MySQL is running first, and start it if it isn’t, then you could add this to the crontab:

    */1 * * * * run-one sudo service mysql start

    PS add this if you want to stop it emailing you the results of every time cron runs the script:

    >/dev/null 2>&1
    */1 * * * * run-one sudo service mysql start >/dev/null 2>&1

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.

Submit an Answer
2 answers


my MYSQL is down 10 times for day, for this reason I need the bash script.

I’ve this 2 bash script in the /root :


ps auxw | grep apache2 | grep -v grep > /dev/null

if [ $? != 0 ]
        /etc/init.d/apache2 start > /dev/null

if [[ ! "$(service mysql status)" =~ "start/running" ]]
    service mysql start

None working properly

You can help me please?
Thank you very much

  • I would recommend finding out why MySQL is crashing in the first place and solving that instead of working around it and just restarting it and then having it crash again.

    Check MySQL’s error log, what’s the output of this command?

    sudo tail -30 /var/log/mysql.err

You can restart MySQL by running sudo service mysql restart. You don’t need a bash script, you can just run that command. Is that what you need?