Checking website status with Bash under Ubuntu

Posted by – July 29, 2011

With few lines of code we can create a very smart bash script that doest the website status check for us, this script uses the ubuntu notification system to display a notification on desktop if the website goes down.

Here’s how you can do this.

First, you need to install required packages:

sudo apt-get install libnotify-bin msmtp openssl ca-certificates

And now configure account in msmtp:

1
2
3
4
5
6
7
8
9
10
11
account default
host smtp.gmail.com
port 587
from me@gmail.com
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
auth on
user me@gmail.com
password gmailpassword
logfile ~/.msmtp

You need to add the read and write permissions ONLY to the owner of the file above with the following command:

chmod u+rw .msmtprc

And then we are ready to create and make use of our script, please copy the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

url="http://www.site.com"

response=$(curl --write-out %{http_code} --silent --output /dev/null ${url})

if [ $response -eq 200 ]
then
    echo "Site up"
else

    echo "To: user@gmail.com"                > down.mail
    echo "From: notifications@site.com"          >> down.mail
    echo "Subject: Site www.site.com.br is down" >> down.mail
    echo ""                          >> down.mail
    echo "The site $url is down!"                >> down.mail

    msmtp -t < down.mail

    notify-send "Site www.site.com is down" "The site $url is down!" -i notification-network-wireless-disconnected
fi
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>