If your web site is hosted on a dedicated server (cloud or physical) then chances are you have some internal processes which need to happen on a recurring basis. The Windows Task Scheduler is a wonderful built-in tool that fulfills this need. The location of this program has changed from Windows Server 2003 to Windows Server 2008. With Windows Server 2003 it was located in the Control Panel. With Windows Server 2008 it is located in Administrative Tools.
With the Windows Task Scheduler you can run any program on the server including custom scripts at any time with any recurring frequency. So this great news for system admins but what happens if you’re a web developer and you designed an admin page on your site to perform some internal housekeeping which runs when the page is loaded? As you can imagine you don’t want to sit at your desk all day hitting the refresh button.
So here’s were the power of Windows Task Scheduler comes into view. We just need to create a new scheduled task to visit the web site. Well unfortunately this is not possible. Task scheduler is not able to browse sites. However, that would be a cool feature for a future release. So are we done before we’ve started? What could be used to open a web site url that we could then in-turn schedule as a task? Well look no further than Microsoft’s XMLHTTP object. I always say “there’s no school like old school” and in this case it is absolutely true.
The following vbscript is all we need to open the web site url programmatically.
On Error Resume Next
Set objRequest = CreateObject("Microsoft.XMLHTTP")
URL = "http://www.peterviola.com/testme.htm"
objRequest.open "GET", URL , false
Set objRequest = Nothing
Just cut and paste the snippet above into a simple .vbs text file on your server and it will be ready to run. If you run it manually it won’t open a browser but the request is completed. To know it works you just need to check your web site logs. With this bit of code we have identified a way to programmatically call web site url from within our server without having to be logged into the server. So looking back at our original “task” we now have all the pieces in place to get the job done.
The next step is to just configure Windows Task scheduler and here again Microsoft makes it easy for us. When you open Task Scheduler on the right side of your screen just click “Create Basic Task” and the Create Basic Task Wizard will launch. Just follow the steps and complete the wizard.
You will be prompted to choose the program you want to run. Use the menu to find the .vbs file you created earlier.
After you complete the wizard your task will be ready to run based on the schedule you picked during the wizard. However in some cases you may want your task to run more frequently than once per day. So using the advanced properties you can choose to repeat the task as frequently as every 5 minutes forever.
As I mentioned above you can confirm it works by checking the www logs for your site. Using the powerful command findstr as shown below I can pull out just the requests I want for my test page:
findstr /S /I /P /c:"GET /testme.htm" C:\wwwlogs\W3SVC1\u_ex120915.log >testme.txt
Here are the results which clearly show the scheduled task is working as expected hitting my test page every 5 minutes.
2012-09-15 18:50:22 W3SVC74 ABC123 x.x.x.x GET /testme.htm - 80
2012-09-15 18:55:22 W3SVC74 ABC123 x.x.x.x GET /testme.htm - 80
2012-09-15 19:00:22 W3SVC74 ABC123 x.x.x.x GET /testme.htm - 80
2012-09-15 19:05:22 W3SVC74 ABC123 x.x.x.x GET /testme.htm - 80
\r\n\r\nThis simple technique can be leveraged in so many powerful ways. Thanks for reading!