Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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=dude@email.net
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 alerts@website.net -t $EMAIL -u ALARM -m < $THEDIR/mail
        rm ~/.apache-was-up
    fi
fi

rm -rf $THEDIR
share|improve this question

3 Answers 3

if [[ -f ~/.apache-was-up ]]

In Ubuntu, /bin/sh is a symlink to dash instead of bash. dash is a minimal shell designed for scripts, whereas bash is more featured and designed for interactive use. The [[ <expression> ]] syntax is only implemented in bash, not in dash, and therefore you receive that error. The [ <expression> ] syntax is enough for what you need though, so change that line to this:

if [ -f ~/.apache-was-up ]
share|improve this answer
    
After copying Jay's script I changed the top line to /bin/bash and it worked fine the first three times I ran it then it just stopped working. –  Tyler Jun 12 '12 at 2:13

I think this is what you're looking for (I prefer /tmp since it's removed after a reboot, so a fresh start).

#!/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=dude@email.net
mkdir -p $THEDIR

if ( wget --timeout=30 -q -P $THEDIR http://website.com/robots.txt )
then
    # we are up
    touch /tmp/.apache-was-up
else
    # down! but if it was down already, don't keep spamming
    if [  -f /tmp/.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 alerts@website.net -t $EMAIL -u ALARM -m < $THEDIR/mail
        rm /tmp/.apache-was-up
    fi
fi

rm -rf $THEDIR
share|improve this answer
    
This works perfectly thank you Jay! –  Tyler Jun 12 '12 at 1:24
    
Hmmm very strange, it worked 3 times and now it doesn't work anymore. –  Tyler Jun 12 '12 at 1:43
    
@Tyler Doesn't work in what way? –  mgorven Jun 12 '12 at 4:20
    
I forgot to change one of the ~/.apache-was-up's. How about now? –  Jay Jun 12 '12 at 10:44

While the script may work, there are some issues with it, like unsafe use of temporary files.

You really should have a look at Monit, its open source and it is easy to write a rule that will restart your web services based on service responses. It can also detect if a running process dies (e.g. its PID goes away) and you have the possibility to temporary deactivate a check (e.g. when you are doing a service maintenance).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.