Created
November 30, 2015 15:34
-
-
Save zayarwinttun/d5e46311afca038eddb8 to your computer and use it in GitHub Desktop.
shell script for httpd, mysql service check and restart on down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#about: checking services, if down ? restart | |
#note: dont forget to add this script to cron for automation | |
#define services to check | |
services=( "httpd" "mysqld" ) | |
for service in "${services[@]}" | |
do | |
if (( $(ps -ef | grep -v grep | grep $service | wc -l) == 0 )) | |
then | |
/etc/init.d/$service restart | |
fi | |
done | |
########## | |
# optional check: if free memory spaces < 100mb ? clear caches | |
if (( $(free -m | grep -i "mem" | awk '{print $4}') < 100 )) | |
then | |
eval $(echo 3 > /proc/sys/vm/drop_caches) | |
fi | |
########## | |
#optional: send an email to system admin when services are restarted | |
#note: set a restart flag to 1 within above code, the service restart if condition. | |
#define time | |
now=$(date +"%F %H:%M:%S") | |
#define system admin's email address | |
email="system@admin.com" | |
if [ $restartflg -gt 0 ] | |
then | |
printf "services are downed and automatically restarted on $now" | | |
mail -s "System Message: service restarted" $email | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment