4

I have a bash script that emails me whenever a web server is not responding, and the script is ran by cron every 5 minutes. However, if the website goes down for a few hours, I'd receive too many messages instead of just one.

What's the best way to make it email only once? Should I use an environment variable and check it before sending the email/resetting it when web server goes up again? Are there better ways to do this (without polluting the environment)? Am I doing something silly right now? I'm not confident in my shell scripting skills.

#!/bin/sh

output=$(wget http://lon2315:8081 2>&1)
pattern="connected"

if [[ ! "$output" =~ "$pattern" ]]
then
    echo "$output" | mail -s "Website is down" "myemail@asdf.com"   
fi
  • 4
    I don't think you can use environment variables, as they won't persist between script "runs". What about writing to a temporary file in /tmp, then checking it each time? – Sparhawk Feb 10 '15 at 10:06
  • @Sparhawk thanks, that sounds like a good idea. However, I see that the uptime for the machine I'm working on is 49 days, yet the oldest file in /tmp/ is 10 days, so I'm thinking they might use other methods for cleaning /tmp/ other than reboot. I guess writing on a file on my own dir should work – confused00 Feb 10 '15 at 10:13
  • 2
    Yes, probably a good idea, especially if it's a shared system, and you can't be sure of the contents of /tmp. – Sparhawk Feb 10 '15 at 10:16
  • 1
    Done, and thanks. I added some code too so I felt like I deserved some rep. :) Let me know if you need help understanding any of it. – Sparhawk Feb 10 '15 at 10:29
  • As the script is an elementary state machine, I'd rather use /var/tmp, /var/cache or /run for caching state variables. /tmp is really a pot for everything and anything. – user86969 Feb 10 '15 at 13:46
6

I don't think you can use environment variables, as they won't persist between script "runs".

Alternatively, you could write to a temporary file in /tmp or somewhere in your home directory, then check it each time?

For example, something like

#!/bin/sh

output=$(wget http://lon2315:8081 2>&1)
pattern="connected"
tempfile='/tmp/my_website_is_down'

if [[ ! "$output" =~ "$pattern" ]]
then
    if ! [[ -f "$tempfile" ]]; then
        echo "$output" | mail -s "Website is down" "myemail@asdf.com"
        touch "$tempfile"
    fi
else
    [[ -f "$tempfile" ]] && rm "$tempfile"
fi
0

It is a bit dirty, but I would put a file in /tmp or some other place which gets removed when the server is back up again.

Maybe something like this:

#!/bin/sh

output=$(wget http://lon2315:8081 2>&1)
pattern="connected"
websitedownfile="/tmp/websitedown"

if [[ ! "$output" =~ "$pattern" ]]; then

    if [[ -e $websitedownfile ]]; then
        echo "$output" | mail -s "Website is down" "myemail@asdf.com"
    fi
    touch $websitedownfile
else
    [[ -f $websitedownfile ]] && rm $websitedownfile
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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