November 17, 2007
If you do alot of web developmentyou may have come across a project that required reports or emails that need to run on a schedule. You could do it all with web code by tracking the intervals your routine should run but that has one major flaw, it requires a visitor to trigger the event. If no one visits the web site the routine gets skipped and so much for your schedule. If your scheduled task is at all important you’re in trouble.
XMLHTTP and Windows task scheduler to the rescue. Windows scheduler is extremely accurate and requires no action from anyone to be triggered. I found everything from scripts that lunched IE resulting in hundreds of open windows on your server to buggy scripts that don’t do anything all over the web, so I gave up looking and wrote one myself and it works just dandy. Works with IIS and Apache and works with php, asp, .net, jsp, heck any web language for that matter. Three easy steps to scheduling bliss.
STEP 1: CREATE YOUR VBS SCRIPT
Open notepad and paste this code then change the file extension to .vbs
Call RunProcess()
Sub RunProcess()
'if an error occurs keep on truckin | write an error handler if you like
On Error Resume Next
Dim URL, objRequest
Set objRequest = CreateObject("Microsoft.XMLHTTP")
'url to page needing a scheduled run
URL = "http://www.yourdomain.com/filename.php"
objRequest.open "POST", URL , false
objRequest.Send
Set objRequest = Nothing 'clean up memory and thanks for playing
End Sub
Step 2: SETUP YOUR SCHEDULED TASK
Open Windows scheduler and add a task, browse to your newly created vbs file and set your schedule up.
Step 3: TEST IT
right click on the task and say run if the result is 0x0 you’re golden otherwise backup and start over.
(requirements: windows OS, any web server, any web page, direct access to scheduled tasks)
Note: If your web page is running an intensive routine remember to set the script.timeout on the page itself so it has plenty of time to complete the chore. The task scheduler is oblivious to your timeout requirements as it just calls the page and closes.
Don’t have direct access to the server you host on? You can always use a third party web based service to setup schedules and run them. I have never tested them as I don’t trust mission critical tasks to other vendors, but I am sure they work fine.
Rock on! Thanks for this great code.
Nice trick. This solves heaps of issues with automating services.
Thanks a lot, this is a great piece of code that helped me to schedule a creation and sending of a pdf file with one call.