Test if your server is up, using Ruby

How do you know if your website is up, right now? Maybe you are browsing it and got an error, or a friend texted to tell you it was down. Otherwise your website might be down and you'd have no idea. Here is a simple script that will help you learn faster whether your website is up or down. I wrote it in Ruby to start learning Ruby syntax.

require 'uri'
require 'net/http'
require 'net/smtp'

def send_email(to, opts={})
    # Send an email. In Ruby, the ||= operator means 'only assign the
    # new value if the value has not been set yet'
    opts[:server]      ||= 'localhost'
    opts[:port]        ||= 25
    opts[:ehlo_domain] ||= 'localhost'
    opts[:username]    ||= 'username'
    opts[:password]    ||= 'password'
    opts[:from]        ||= 'youremail@domain.com'
    opts[:from_alias]  ||= 'First Last'
    opts[:subject]     ||= 'Test email'
    opts[:body]        ||= 'This is a test!'

    # the following says 'create a new string called msg containing everything
    # from here to the END_OF_MESSAGE marker'
    # then, we fill in the email with all of the values we filled in above. Note
    # that there has to be a newline between the subject and the body of the
    # message.
    # This is a plaintext message - to send HTML emails, we would need a more
    # complex template.
    msg = <<END_OF_MESSAGE
From: #{opts[:from_alias]} <#{opts[:from]}>
To: <#{to}>
Subject: #{opts[:subject]}

    Net::SMTP.start(opts[:server], opts[:port], opts[:ehlo_domain],
                    opts[:username], opts[:password], :plain) do |smtp|
        smtp.send_message msg, opts[:from], to

# test for main method
if __FILE__ == $0
    # the domain we want to check
    url = 'http://testdomain.com'
    my_email = "myemail@gmail.com"
    r = Net::HTTP.get_response(URI.parse(url))
    if r.code != "200"
        # we have a big problem, server is down or other
        opts = {
            :server     => 'smtp.yoursmtpserver.com',
            :port       => 587,   # should be 25 or 587
            :ehlo_domain => 'localhost',      #this doesn't matter too much
            :username   => 'yoursmtpusername',
            :password   => 'yoursmtppassword',
            :from_alias => 'testdomain.com Admin',
            :subject    => 'The website is down',
            :body       => "Hey,\nan attempt to reach the homepage returned " + 
            "the status code " + r.code.to_s() + ". \n\nYou should check it out."
        send_email my_email, opts
That will send an HTTP request to your website. If the request was successful, you'll get an HTTP Header Response that says 'Status Code: 200 OK'. If we don't get a 200, this means the site is definitely wrong. We may get a 200 and still have problems with the database or other, but this is meant to be a simple test. Other well known codes are 404 (File Not Found), 500 (Internal Server Error), and 418 (I'm a teapot). Once you've got the script in place, save it as "is_server_up.rb" somewhere on a computer which has lots of uptime (your school's CS server, a remote SSH server, or otherwise). Your laptop is a bad choice because when you close your laptop, or are not connected to the Internet, the test will not be able to run. The computer hosting your website is also a bad choice because if that computer goes down, your website will be down but the script will not run. On a Linux or Mac machine, type
$ crontab -e
which should open up a Terminal text editor. Then type:
0-59/2 * * * * /usr/local/bin/ruby /path/to/your/file/is_server_up.rb
is the path to the Ruby executable on your laptop. If you don't know where it is, try typing "which ruby" in the Terminal. If nothing comes up, you probably need to download Ruby. The
bit says to run the script every 2 minutes. You might be worried that fetching your homepage every 2 minutes will generate an extra 720 hits to your site every day and affect your Analytics stats. However, Analytics will only measure visits from browsers that can execute Javascript, and this Ruby script will not execute any Javascript, so you are safe. Note that if you have an important site, or one which generates revenue, you probably should not use this tool - there are better tools like Pingdom that will let you know right away whether your site is down.

Liked what you read? I am available for hire.

Leave a Reply

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

Comments are heavily moderated.