touch monitorservice.sh
#!/bin/bash
netstat -ant | grep 3306 /dev/null#'-ant display only TCP ports
MYSQLSTATUS=`echo $?`
SERVICENAME="mysqld"
COUNT=0
THRESHOLD=2
if [ $MYSQLSTATUS != 0 ]
then
while [ $COUNT -le $THRESHOLD ]
do
service $SERVICENAME start
if [ $? != 0] # if the service is not started
then
let "COUNT += 1"
else
exit 0
fi
done
echo Problems starting $SERVICENAME | mail -s "$SERVICENAME FAIL" root
else
echo $SERVICENAME is running
fi
#END
. monitorservice.sh - runs ok if the service is running
chamod u+x monitorservice.sh
./monitorservice.sh -> returns mysqld is running
service mysqld stop
./monitorservice.sh -> returns Stating MySQL
In order for the script to send email to administrator change the SERVICENAME to something that does not exist
./monitorservice.sh -> returns Problems starting SERVICENAME and sends emails
No comments:
Post a Comment