Skip to content

Instantly share code, notes, and snippets.

@zayarwinttun
Created November 30, 2015 15:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zayarwinttun/d5e46311afca038eddb8 to your computer and use it in GitHub Desktop.
Save zayarwinttun/d5e46311afca038eddb8 to your computer and use it in GitHub Desktop.
shell script for httpd, mysql service check and restart on down
#!/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