Skip to content

Instantly share code, notes, and snippets.

@cs-sonar
Created July 29, 2013 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cs-sonar/6102514 to your computer and use it in GitHub Desktop.
Save cs-sonar/6102514 to your computer and use it in GitHub Desktop.
netstatとかロードアベレージの閾値を超えたらログを残しつつApache再起動
#!/bin/sh
######
# netstatの80が400越えたら再起動
#####
apacheprocess=`netstat -an | grep :80 | wc -l`
if [ $apacheprocess -gt 400 ]; then
stopdate=`date`
echo "$stopdate apache stoped. try restart." >> /usr/local/hosting/apache_restart_log.txt
netstat -an > /usr/local/hosting/apache_restart_netstat.txt
/usr/local/apache2/bin/apachectl stop
echo "$stopdate apache stoped (done)" >> /usr/local/hosting/apache_restart_log.txt
sleep 60
/usr/local/apache2/bin/apachectl start
echo "$stopdate apache started." >> /usr/local/hosting/apache_restart_log.txt
echo 'apache netstat restart' | mail mailaddr@example.com -s "Apache Restart "`hostname`
fi
######
# ロードアベレージが50越えたら再起動
#####
LOAD=`cat /proc/loadavg | awk '{print $1}' | sed 's/\..*//'`
if [ $LOAD -gt 50 ]; then
stopdate=`date`
echo "$stopdate loadAvg is too high. apache stopping." >> /usr/local/hosting/apache_restart_log.txt
/usr/local/apache2/bin/apachectl stop
echo "$stopdate apache stoped (done)" >> /usr/local/hosting/apache_restart_log.txt
sleep 60
/usr/local/apache2/bin/apachectl start
echo "$stopdate apache started." >> /usr/local/hosting/apache_restart_log.txt
echo 'apache LA restart' | mail mailaddr@example.com -s "Apache Restart "`hostname`
fi
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment