MySQL process check with cron

I can run this script fine from the terminal and I get the else but when I have cron set to run it every 5 minutes I am sent a email and the mysql server is restarted every 5 minutes. So I am guessing that the cron job does not have the correct permission to see if the process is running, so it restarts the server and sends an email. How would I get this script to execute from cron and be able to check if mysql process is running?

UP=$(service mysql status | grep 'mysql start/running' | wc -l);
if [ "$UP" -ne 1 ];
        sudo service mysql start
        cat /var/db-backup/mysql_restart_email | msmtp
        echo "All is well.";

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

when you set up the cronjob, are you running:

sudo crontab -e

Here is my take on the same idea, but I made it so the script checks on other services (like Apache2)

works fine on all my DO droplets for the last several months.