Bash Script to Find Out If MySQL Is Running Or Not
Overview
This is a simple solution to monitor the MySQL daemon, if it detects that the daemon is not running, it starts it and send a notification email.
It is a simpler solution than using other more complete software like supervisord or MonIT.
Creating the script
Create the script /root/scripts/monitor_mysql.sh
with the following
content:
#!/bin/bash
##########
# Config #
##########
mysql_daemon='mysqld'
pgrep='/usr/bin/pgrep'
mysql_start='sudo service mysql start'
fail_msg="MySQL is down in $(hostname)."
##########
# Script #
##########
#look up process
$pgrep $mysql_daemon > /dev/null
if [ $? -ne 0 ]; then
echo $fail_msg
$mysql_start
fi
And make it executable:
# chmod +x /root/scripts/monitor_mysql.sh
Cronjob
Specify through crontab
to run it every 10 minutes. Running
# crontab -e
Add the following lines:
MAILTO=mail@example.com
*/10 * * * * /root/scripts/monitor_mysql.sh
Every time the script starts the daemon, an email will be sent with its output.
cron(8) looks at the MAILTO variable if a mail needs to be send as a result of running any commands in that particular crontab. If MAILTO is defined (and non-empty), mail is sent to the specified address. If MAILTO is defined but empty (MAILTO=""), no mail is sent. Otherwise, mail is sent to the owner of the crontab.
Reference
- Crontab http://man7.org/linux/man-pages/man5/crontab.5.html
- Simple bash script to check whether MySQL is running
- Connect to a Bluetooth device from command line in Ubuntu LinuxJune 23, 2020
- Add Infolinks Script To An Existing Website From Console With Sed CommandApril 4, 2017
- How to change all files permissions to 644 and directories to 755January 10, 2017
- Shell Redirect Output And Errors To The Null Device In BashDecember 9, 2016
- Prevent Running Of Duplicate Cron JobsDecember 8, 2016
- Delete All Backup Files Recursively In BashNovember 28, 2016
- Bash Script to Find Out If MySQL Is Running Or Not
Articles
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·