Use a Mac to Monitor Website Uptime or Other Regular Tasks
Time flies by no matter what you do. Using some automation techniques, you can get time to work for you. In this tutorial, I'll show you how to use two programs to run activities based on time: Alfred Cron workflow and Clockwise.
In order to see how these programs work, I will automate the checking of your web site's status.
Installing the Programs
Clockwise is available in the Mac App Store. It is really easy to use and makes the creation and editing of timed tasks really easy. Clockwise makes use of AppleScript and shell scripts to extend functionality.
If you are not up to speed on AppleScript, please check out the tutorial The Ultimate Beginners Guide to AppleScript. I will be writing the script in Ruby. If you want to learn more about Ruby, check out this series on Net Tuts+.
In order to use Afred Cron by Shawn Patrick Rice, you have to have Alfred version 2.3 and a Powerpack license. You can download Alfred Cron from Packal. When it is downloaded, double click on the workflow and Alfred will install it for you.
If you own a web site, you will want to make sure it’s up all the time. There are for pay services that you could use, but not every blog merits a paid service just to monitor it. That’s where doing your own monitoring can help.
The easiest way to check for a site is with this simple Ruby script:
#!/<your ruby installation location>/bin/ruby # # Include libraries used. # require 'open-uri' # # Data Storage files defined. These files contain # a single number for quick processing. # countFile = "/<your data file location>/siteCount.txt" UpcountFile = "/<your data file location>/UpCount.txt" # # Get the raw number of counts and increment it. # count = IO.readlines(countFile) count = 1 + count.to_i IO.write(countFile,count) # # Get the number of the site being live count and # increment it if and only if it is currently alive! # upcount = IO.readlines(UpcountFile) # # The exception handling tells if the site is alive. If the # site is not reachable, the "open" to read the site will # throw an exception. # begin source = open("<your web site address>").read puts "Up" upcount = 1 + upcount.to_i rescue puts "Not up" end # # Write the Upcount to the file. # IO.write(UpcountFile,upcount) # # If the upcount lags behind too much, say that the site is down. # if (count - upcount) > 10 then `/usr/bin/osascript -e 'display notification "Site is Down!"'` end
Save this to the file name checkWeb.sh in the home directory.
This script simply increments a number in a data file for the number of times it has run and the number of times the site was alive.
You just need to change <your ruby installation location> to the location of Ruby on your system. The default location for most Macs is the
usrdirectory. To see where Ruby is, you can type this in the Terminal.
Copy the path this gives into the file.
You'll also need to change the <your data file location> to a directory of your choice, and <your web site address> to the web address of the site you wish to check.
I created a directory in my home directory called data for the data files. Now, open a terminal to your home directory and type:
You should get an output of Up if your site is up. The script will also increase the counts in the data files.
If the up count lags behind the total number of counts by ten, the user gets a system notification. Unfortunately, this approach is manual since you have to launch it by hand. To be useful, this needs to automatically run every ten minutes or so.
The Clockwise application makes the creation of timed events very easy to do. Once you have it on the system, it sits in the menubar area.
When you click on it’s hourglass icon, it shows you all of the timed events you have with it’s next trigger time. To create a new trigger, select the + at the bottom left corner. You can name it Site Check Timer.
Set the Timer to trigger every 10 minutes with the Run Forever option selected. Click the right pointed arrow in a circle under the clock on the left.
This is the Actions for this event area. Click the + in the bottom right corner to add a new Action. Clicking the top bar gives a full list of possible actions.
You can set up many types of actions here. Each event can run any number of actions. The one you want is Run user script.
In the Available scripts area, you will see a list of every script that Clockwise can run. You need to add a new script.
Therefore, click on the i in a circle to open the dialog for the Open folder option.
This will open a Finder window in the directory for scripts. Since Clockwise is a sandboxed application, it can only run scripts in this one directory.
Copy the script for checking the web site to this directory.
Select the script in the Available scripts dialog. Select the check mark at the top right of the dialog.
You will then see the Actions for this event with the script selected to run.
Click on the check mark at the top right of the dialog again.
You will now see all of your timer events with the time till they will trigger. The Site Check Timer will trigger in 4 minutes and 50 seconds.
When that time has elapsed, you will see the data files have the count numbers increased in them. The UpCount.txt file will only increase if the site checked was reachable. If the up count lags behind by more than ten counts, you will get a notification every ten minutes until you reset the count files.
You have just mastered time using Clockwise.
You might already have Alfred and you do not want to buy another program. You can do the same thing with Alfred Cron. Once you install the workflow, type cron in the Alfred Prompt.
First, you need to select Add a Cron Entry to create a new cron job. Cron is a timer that is a part of any unix style system.
On the Mac, it is really controlled by launchd agent. It is what Clockwise makes use of also, just a little more transparently. Alfred Cron just gives a nice frontend to the background launchd daemon.
Set up the new job just as you see here, Label is Site Checker Timer, Execution Interval is 600 seconds, and the script is the same as before. You now have new way to check your web site.
The jobs with Alfred Cron only allow you to set a delay time between triggering events, while Clockwise gives you many more options. It all depends on what your needs are as to which one is the best.
Now that you have tools to manage tasks on a time table, go and master them.
Experimenting with different tasks is a lot of fun. For an exercise, make the counting script an Alfred workflow accessed by a timer using the External Trigger block. That’s an easy modification that’s easy to maintain.