How can I automatically restart Apache and Varnish if can't fetch a file?

Posted by Tyler on Server Fault See other posts from Server Fault or by Tyler
Published on 2012-06-12T01:03:13Z Indexed on 2012/06/12 4:42 UTC
Read the original article Hit count: 546

Filed under:
|
|

I need to restart Apache and Varnish and email some logs when the script can't fetch robots.txt but I am getting an error

./healthcheck: 43 [[: not found

My server is Ubuntu 12.04 64-bit

#!/bin/sh
# Check if can fetch robots.txt if not then restart Apache and Varnish
# Send last few lines of logs with date via email

PATH=/bin:/usr/bin
THEDIR=/tmp/web-server-health
[email protected]
mkdir -p $THEDIR

if ( wget --timeout=30 -q -P $THEDIR http://website.com/robots.txt )
then
    # we are up
    touch ~/.apache-was-up
else
    # down! but if it was down already, don't keep spamming
    if [[ -f ~/.apache-was-up ]]
    then
        # write a nice e-mail
        echo -n "Web server down at " > $THEDIR/mail
        date >> $THEDIR/mail
        echo >> $THEDIR/mail
        echo "Apache Log:" >> $THEDIR/mail
        tail -n 30 /var/log/apache2/error.log >> $THEDIR/mail
        echo >> $THEDIR/mail
        echo "AUTH Log:" >> $THEDIR/mail
        tail -n 30 /var/log/auth.log >> $THEDIR/mail
        echo >> $THEDIR/mail
        # kick apache
        echo "Now kicking apache..." >> $THEDIR/mail
        /etc/init.d/varnish stop >> $THEDIR/mail 2>&1
        killall -9 varnishd >> $THEDIR/mail 2>&1
        /etc/init.d/varnish start >> $THEDIR/mail 2>&1
        /etc/init.d/apache2 stop >> $THEDIR/mail 2>&1
        killall -9 apache2 >> $THEDIR/mail 2>&1
        /etc/init.d/apache2 start >> $THEDIR/mail 2>&1
        # prepare the mail
        echo >> $THEDIR/mail
        echo "Good luck troubleshooting!" >> $THEDIR/mail
        # send the mail
        sendemail -o message-content-type=html -f [email protected] -t $EMAIL -u ALARM -m < $THEDIR/mail
        rm ~/.apache-was-up
    fi
fi

rm -rf $THEDIR

© Server Fault or respective owner

Related posts about linux

Related posts about ubuntu