Note: This article is Part 4 of the IIS Tutorial Series. Although this article does not relate to IIS, IIS is probably the primary web server the user reading this article is using.
Running a re-occurring task on a web server on a periodic basis without interaction from the client is sometimes necessary. Such as to send a daily notification email, or run a statistic processor. UNIX-like Operating Systems have the well-known Cron as a simple option. On Windows, it’s Task Scheduler. In this tutorial, I will step through the process of setting up a scheduled task to have a web script executed periodically. It’s also important that you have a Windows User login with a password that the task can execute under. For this tutorial, here’s an overview of how the task execution process will work:
- A task scheduler is set up to run once every day.
- The task is to execute a batch file, which is simply a text file containing a list of commands.
- A command in the batch file is to call Wget, a program that retrieves content from web servers (like a web browser), and tell it to call the URL of your web script.
Note: You might be thinking why we are calling the web script through a publically-accessible URL rather than a local system path, and the reason is to keep this tutorial simple. Calling a local path requires calling the PHP interpreter, which is a bit more complicated. There are also the problem of the current execution path of the script, which may require adjustments to any relative paths you use in your script (such as include() calls). So in this tutorial, we’ll execute a publically accessible URL, and let the web server take care of the rest.
Note 2: We could just have the Task Scheduler execute Wget directly without the batch file, but I’ve added it because the batch file can contain more than one command we can execute. However, in this tutorial we will only execute one command.
Creating a script to execute periodically
We’ll keep this very simple, and create a script that we can see results . Create the following PHP script.
mail('[email protected]', 'My Subject', 'Hello World!');
Replace the email address with your own. Save the file as helloworld.php. This will just send out an email to your email address with “Hello World” as the content. Put the file on your web server so that you can access it through a publically available URL. For example: http://test.com/helloworld.php
Install Wget For Windows
Wget is the program that will call the URL (which the web server will execute). Download it, and install it on your web server. To make it easier to execute Wget in the batch file without requiring the full path to the Wget executable, add Wget to the path of your system. For more information on how to do that, visit this page on How to set a path in Windows. (Even if you’re not using Windows 2000/XP, most Windows version applies.) Add the Wget program directory’s “bin” folder, which for most default install is: C:\Program Files\GnuWin32\bin
Setting up the batch file
Create a batch file and name it run.bat. Save it to a location on you web server that is not in a public path. For example (to keep it very simple): C:/scripts/run.bat
Open up the batch file and add the following, where test.com is your domain name.
wget -q -O nul http://test.com/helloworld.php
This basically tells Wget to call that URL (to your script), and don’t output anything nor save to a file.
Setting up task scheduler
Add a new scheduled task by going to Scheduled Tasks on your web server. It should be located in the Control Panel. Double-click on Add Scheduled Task. Continue until it asks you to browse for a program to execute. Click on “Browse…” and look for and select the run.bat file you created. Continue with the options, enter a name for you task, and select to perform the task Daily, and set the time to 10 minutes from the current time. Continuing on, it will ask for a Windows username and password the task will execute under. After that, you’ll reach the end, and that’s it! You can also opt to view more advance options. With the advanced options, you can even set multiple schedule times under the single task. For example, you want it to run the task once an hour between 8am to 5pm, but only once every four hours thereafter. If you haven’t changed the advanced options, wait around 10 minutes, and your script should execute. The main screen of the Scheduled Tasks will also display when the last execution time was.
Now, all that’s left is to turn that script into something useful. The script is like any normal web script and it can do anything you can make it do, such as checking a database for deadlines occurring within one day, and sending out email reminders, etc. Since the URL to the script is public, you might want to add some kind of hard-to-guess preset token to the query string of your URL, and your script checks that it’s a match before executing it.