Cron for Windows IIS
General December 4th, 2006First, 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 learn. You have probably found that several web sites offer programs that cost around $100 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:
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
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
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
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
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
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, all your are doing is utilising Windows’ “Scheduled Tasks” program - using a command line interface, which I find easier to use. 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, your 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.
I hope this has helped you out!
The alternative of course is to host on a linux server, like this great host offers