Homemade Automatic Domain Tester with PhantomJS Part 2

Previously on Bitpi

If you have stumbled across this post and haven't read part one, check it out. Also, the code can be found here.

Reason Why

I had an idea to write a program that automatically checked a list of websites to make sure they were still running. The idea is that I could later modify this program to contact me if a site was down.

Tools within My Reach

Things to do this time around

In this update, we are going to set up a few things. - Add a time output that tells us how long the program took to run. - Hook the program up to a cron job.

Adding a Timer

Grab the current time at the beginning of the program.

var mydate = new Date();
var now = mydate.valueOf();
var startTime = now;

Grab the time again at the end of the program

var endTime = new Date().valueOf();

Subtract the start time from the end time to get the total run time.

var timeDiff = endTime - startTime;

Convert from milliseconds to seconds. (if you'd like)

var timeInSecs = timeDiff/1000;

Display the output in the console.

console.log('Test suite took ' + timeInSecs + ' seconds to complete');

Here is what you should see after the program runs..

View of timer after the domain tester has ran

Automatically running program on a schedule using crontab

Crontab is an essential tool that is included with most linux distros. It can be used to run programs or commands on a schedule. Whether that is every 5 seconds or once a year, that's up to you!

Get back to the command line and type..

crontab -e

This will open up your command line scheduler.

View of an empty crontab

It's likely that every line will be commented out if you have yet to use crontab before on your system. We are going to add a line that does not have a hashtag at the beginning.

0 * * * * phantomjs  /home/pi/tests/domain_tester.js

This will run our domain tester on the hour, every hour.

Possible next steps

The next step for me on this little project will most likely be some sort of log file. There isn't a whole lot of point to me running this on cron in the background if I'm blind to the status of the tests. So look out for a part 3 as my adventure continues.

Check out Part 3 of the Domain Tester!