Cron jobs for IIS

Recently I’ve had to work on Drupal and phpBB and that has meant quickly learning a lot about PHP. What a pleasant experience!  It is an amazingly productive language and it is easy to get results quickly.  On the other hand, working with Windows servers mean there are few things to deal with that are better understood in the Unix world.  One which keeps cropping up is the need to execute code on the web site at regular intervals.

This is commonly called a “cron job”, referring to the usual technique employed in Unix to get something done automatically.  There are ways to get this done within the web pages themselves.  In phpBB, as an example, one could call a function on every page and have it only perform the necessary operations when the right time has passed.  However sometimes these operations are going to be very time consuming.  The result is that the user browsing the web site who happens to be unlucky enough to trigger the operation has to wait during a page load and that isn’t a good experience.

The best option is to put your code in a specific page and have some means to schedule a “view” of that page.  Unless you want to fiddle with access control, the page needs to be written to handle unnecessary views smoothly and not to return any sensitive information in the output (should someone decide to call it from a regular browser).

My approach is fairly generic and easy to implement on Windows servers: create a Javascript file that calls a web page and use the task scheduler to execute it.

We start with a script. Open notepad and paste this into it:

var xmlDoc = new ActiveXObject("Microsoft.XmlHttp");
xmlDoc.open("GET", WScript.Arguments.Item(0), false);
xmlDoc.send();
WScript.Echo(xmlDoc.responseText);

Then save this somewhere as, say “C:\cron.js”. No go to the task scheduler and add a new task.

Create a New Task in Windows 2003

I’m using Windows 2003 in the above example but the idea is fairly similar in Windows 2008.

Windows 2003 New Task Wizard

You’ll need to select “cscript.exe” as the program to schedule and this usually lives in “c:\windows\system32\”.

Select cscript as the program to run

The next step is deciding when the schedule it. If you want to have the operation run more often than once per day then select “daily”. I’ll show you how to schedule the operation every five minutes.

Enter a descriptive name and set the schedule type

Select the time the first occurrence will start during the day. Select Every Day if you need more than one occurrence per day.

Next you’ll need to enter the credentials for the account the task will run under. This is necessary because the operation will occur even when nobody is logged in.

Enter the credentials for the user account this task will run under. An administrator account would be best.

Check “Open advanced properties…” before clicking “Finish”

Windows 2003 Task Wizard Finish

Check the "Open advanced properties..." before finishing

In the “Run:” section add two parameters. The first parameter is the full path to the “cron.js” file you created earlier. The second parameter should be the full URL to the cron page you wish to execute.

For example, you might use

cscript.exe "c:\cron.js" "http://www.example.com/cron.php"

Surround the parameters with quotation marks just in case your paths includes spaces.

Set the Run command

If you need your operation to run more often than once per day, here is how you do it. Click on the “Schedule” tab and then click “Advanced”.

Check “Repeat task” and set it to run as often as you want. Check “Duration” and set it to 24 hours.

Check Repeat Until and set the duration to 24 hours

Now you can click “OK” on this dialogue and and then “OK” on the task properties dialogue to finalise the task. You’ll probably have to re-enter your credentials.

Don’t forget to right click on the task and run it manually to make sure everything works.

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>