James Crooke - Web Developer

Cron for Windows IIS

Posted by James on Friday, January 16th, 2009

This is a slightly updated repost of an article I wrote on my old blog. I have had a few requests from people and one from birf to put it back up so here it is;
First, a lesson in the real implementation of cron…
The crontab command, found in Unix operating systems, is used to schedule [...]

continue reading

Cron for Windows IIS

Posted by James on Friday, January 16th, 2009

This is a slightly updated repost of an article I wrote on my old blog. I have had a few requests from people and one from birf to put it back up so here it is;

First, a lesson in the real implementation of cron…

The crontab command, found in Unix operating systems, is used to schedule commands to be executed periodically. It reads commands and collects them into a file also known as a “crontab” which is later read by the operating system which carries out the instructions contained within.

One of the most useful cron commands, particularly from a web developers point of view, is the ability to call up a webpage silently, in the background, at intervals or at a specified time. Think of the possibilites:

  • Running a routine check on users in a database (deleting inactive accounts for example)
  • Scheduling an email, several days after a user has purchased an item from a site
  • Checking a site is functioning correctly

So how can you get Windows to perform cron-like tasks?

It’s very simple once you know how - which I do, you’ll be happy to read. You have probably found that several web sites offer programs that cost around £50 that promise to emulate cron.

Don’t bother; especially if all you want to do is call up a web address.

This is how you do it…

  • Download wget (GnuWin32 is a good, clean, harmless one)
  • Install wget
  • Fire up the command line (Start > Run > then type “cmd”)
  • Run a command as shown below:
1
schtasks /create /tn "Test Cron" /tr "C:wget.exe http://www.site.com/cron.asp" /sc minute /mo 5 /ru "System"

Where

“Test Cron” is the name of your cron job (useful for reference purposes)
“C:wget.exe” is the location of the wget.exe file
http://www.site.com/cron.asp is the URL for wget to fetch

More Examples

Let’s take a look at some more examples, that way you will understand the different options available…

Run a cron job every week

1
schtasks /create /tn "Test Cron" /tr "C:wget.exe http://www.site.com/cron.asp" /sc weekly /ru "System"

Run a cron every week on a Friday

1
schtasks /create /tn "Test Cron" /tr "C:wget.exe http://www.site.com/cron.asp" /sc weekly /d FRI /ru "System"

Run a cron every friday at 11:00am

1
schtasks /create /tn "Test Cron" /tr "C:wget.exe http://www.site.com/cron.asp" /sc weekly /st 11:00:00 /d FRI /ru "System"

Run a cron every day at 17:00pm

1
schtasks /create /tn "Test Cron" /tr "C:wget.exe http://www.site.com/cron.asp" /sc daily /st 17:00:00 /ru "System"

Run a cron every hour, on the hour

1
schtasks /create /tn "Test Cron" /tr "C:wget.exe http://www.site.com/cron.asp" /sc hourly /st 00:00:00 /ru "System"

So what’s happening exactly?

Well as you might have guessed, all you’re doing is utilising Windows’ “Scheduled Tasks” program - using a command line interface, which I actually find easier to use than the Scheduled Task UI. If you have installed wget and ran one of the commands above, you will be able to see your cron in action by going to “Start > Programs > Accessories > System Tools > Scheduled Tasks”.

And of course, you’re not just limited to fetching URL’s (using wget) - you can also execute a whole load of other programs - providing you know the right commands (or parameters, or both)!

The best thing about this Windows Cron method is you don’t need to have access to your web server for it to work - just any PC that is connected to the Internet (note: it needs to be connected when the Cron is scheduled to “fire”!). It’s also free and, because it runs as standard in Windows, it doesn’t use up any additional memory resources, which is one of the reasons birf loves it.

I hope this has helped you out.

The alternative of course is to use Linux…

Posted in: Featured, Servers.

3 Responses to “Cron for Windows IIS”

  1. Anup Says:

    Great article, thanks for posting this.

  2. Kevin Says:

    Is this similar to using the “At” command already built into windows? http://support.microsoft.com/kb/313565

  3. James Says:

    Yes, the “at” command is almost identical I believe.

Leave a Reply