Skip to content

Instantly share code, notes, and snippets.

@MikeDre
Last active August 6, 2019 11:57
Show Gist options
  • Save MikeDre/6e6e307853a9580ec6c174661b37e7a0 to your computer and use it in GitHub Desktop.
Save MikeDre/6e6e307853a9580ec6c174661b37e7a0 to your computer and use it in GitHub Desktop.
#!/bin/bash
UP=$(pgrep mysql | wc -l);
if [ "$UP" -eq 0 ];
then
curl -X POST -H 'Content-type: application/json' --data '{"text":"⚠️ MySQL Database is down [*SITE_NAME*]"}' https://hooks.slack.com/services/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;
systemctl restart mysql;
curl -X POST -H 'Content-type: application/json' --data '{"text":"🚀 MySQL Database has been restarted [*SITE_NAME*]"}' https://hooks.slack.com/services/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;
else
echo "MySQL Database is running [*SITE_NAME*]";
fi
  1. Create script
    db_monitor.sh
UP=$(pgrep mysql | wc -l);
if ["$UP" -eq 0];
then
        curl -X POST -H 'Content-type: application/json' --data '{"text":"MySQL Database is down [SITE_NAME]"}' https://hooks.slack.com/services/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;
        systemctl restart mysql;
        curl -X POST -H 'Content-type: application/json' --data '{"text":"MySQL Database has been restarted [SITE_NAME]"}' https://hooks.slack.com/services/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;

else
        echo "MySQL Database is running [SITE_NAME]";
fi
  1. Add db_monitor.sh to server
    (e.g. /etc/scripts/db_monitor.sh)

  2. Open
    crontab -e

  3. Add in script
    */5 * * * * /etc/scripts/db_monitor.sh

  4. Save and check cron jobs
    crontab -l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment