Difference between revisions of "Apache Restart"
From James Dooley's Wiki
(→Script) |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| + | [[Category:Scripts]] | ||
==Overview== | ==Overview== | ||
| Line 5: | Line 6: | ||
==Script== | ==Script== | ||
| − | < | + | <source lang='bash'> |
#!/bin/bash | #!/bin/bash | ||
| Line 14: | Line 15: | ||
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 48: | ||
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 | ||
| Line 113: | Line 126: | ||
;; | ;; | ||
esac | esac | ||
| − | </ | + | </source> |
==What to change== | ==What to change== | ||
Latest revision as of 14:30, 25 March 2014
Overview
Restarts apache if load gets above a certain threshold. Has logging and locking functionality.
Script
#!/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.