Difference between revisions of "Apache Restart"
(→Script) |
(→Script) |
||
| Line 14: | Line 14: | ||
LOGROTATE=1000 | LOGROTATE=1000 | ||
| + | #Email Alerts | ||
| + | EMAILTO="" | ||
function plog { | function plog { | ||
| + | |||
echo "[ `date` ][ $LOAD ] $1" >> /var/log/apacherestart | echo "[ `date` ][ $LOAD ] $1" >> /var/log/apacherestart | ||
| + | if [ EMAILTO ] | ||
| + | then | ||
| + | echo "[ `date` ][ $LOAD ] $1" | ||
| + | fi | ||
| + | |||
if [ "`wc -l /var/log/apacherestart | awk '{print $1}'`" -gt "$LOGROTATE" ] | if [ "`wc -l /var/log/apacherestart | awk '{print $1}'`" -gt "$LOGROTATE" ] | ||
then | then | ||
| Line 39: | Line 47: | ||
else | else | ||
echo "What time would you like to set the cron to" | echo "What time would you like to set the cron to" | ||
| − | echo "[IE: */ | + | echo "[IE: */1 * * * * ]" |
read crontime; | read crontime; | ||
if [ ! $crontime ] | if [ ! $crontime ] | ||
then | then | ||
| − | crontime="*/ | + | crontime="*/1 * * * *" |
fi | fi | ||
echo "SHELL=/bin/bash" > /etc/cron.d/apacherestart.sh | echo "SHELL=/bin/bash" > /etc/cron.d/apacherestart.sh | ||
| + | if [ $EMAILTO ] | ||
| + | then | ||
| + | echo 'MAILTO="$MAILTO"' > /etc/cron.d/apacherestart.sh | ||
| + | fi | ||
echo "$crontime root $(readlink -f $0)" >> /etc/cron.d/apacherestart.sh | echo "$crontime root $(readlink -f $0)" >> /etc/cron.d/apacherestart.sh | ||
chmod 0644 /etc/cron.d/apacherestart.sh | chmod 0644 /etc/cron.d/apacherestart.sh | ||
Revision as of 15:08, 24 June 2011
Overview
Restarts apache if load gets above a certain threshold. Has logging and locking functionality.
Script
[javascript,n]
- !/bin/bash
- Load Threshold for doing a dump.
THRESH=20
- Number of lines to keep in log
LOGROTATE=1000
- Email Alerts
EMAILTO=""
function plog {
echo "[ `date` ][ $LOAD ] $1" >> /var/log/apacherestart if [ EMAILTO ] then echo "[ `date` ][ $LOAD ] $1" fi
if [ "`wc -l /var/log/apacherestart | awk '{print $1}'`" -gt "$LOGROTATE" ] then sed -i -e "1d" /var/log/apacherestart fi return }
function restartapache { plog "Restarting Apache" echo $$ > /var/run/.apacherestart service httpd restart rm -f /var/run/.apacherestart plog "Apache Restart Completed" return }
function enablecron { if [ -e "/etc/cron.d/apacherestart.sh" ] then echo "Cron already enabled, use change to set new time" else echo "What time would you like to set the cron to" echo "[IE: */1 * * * * ]" read crontime; if [ ! $crontime ] then crontime="*/1 * * * *" fi echo "SHELL=/bin/bash" > /etc/cron.d/apacherestart.sh if [ $EMAILTO ] then echo 'MAILTO="$MAILTO"' > /etc/cron.d/apacherestart.sh fi echo "$crontime root $(readlink -f $0)" >> /etc/cron.d/apacherestart.sh chmod 0644 /etc/cron.d/apacherestart.sh echo "Cron enabled [$crontime root $(readlink -f $0)]" return fi return }
function disablecron { if [ -e "/etc/cron.d/apacherestart.sh" ] then rm -f /etc/cron.d/apacherestart.sh echo "Cron disabled" else echo "Cron not enabled" fi
return
}
function checkload { LOAD=`cat /proc/loadavg | awk '{print $1}' | awk -F '.' '{print $1}'`
if [ $LOAD -gt $THRESH ] then if [ ! -e "/var/run/.apacherestart" ] then restartapache else opid=`cat /var/run/.apacherestart` if [ ! "`ps ax | grep $opid | grep ${0##*/}`" ] then plog "Stale Lock File Removed" rm -f /var/run/.apacherestart restartapache else plog "Active Lock File Found" fi fi fi }
case $1 in
--cron)
case $2 in
on) enablecron ;; off) disablecron ;; change) disablecron enablecron ;; esac
;;
--help)
echo "Apache Restart usage:"
echo " --cron [on, change, off]" echo " on: Turns on cron job and asks for time" echo " change: Changes the cron time" echo " off: Turns off the cron job"
;;
*)
checkload
;;
esac
What to change
THRESH load threshold before trying to restart apache
LOGROTATE number of lines to keep in the log before rotating.