…posted by davidjmedlock:
Here’s one that was requested when I polled our audience to see what articles our readers would like to see.
If you are on a Unix-based server, you can usually access your server via telnet and set up cron jobs that will automate processes for you. For example, you may want to create a mail queue that retrieves a list of emails to send from the database or a directory and then send the emails. This is essential if you’re sending out a lot of emails, as in a mailing list or newsletter application.
In this situation you may want the email sent out every night, every 30 minutes, or on some other interval. You don’t want to have to log in to your system and push a button. So, you need a scheduled task to perform this work for you.
The problem with doing this is that most of us run our websites from shared hosts where we don’t have easy access to the ColdFusion Administrator, where we would normally schedule tasks for automatic execution.
Have no fear! You can do this with a nifty little tag called CFSCHEDULE. It’s very simple to use, as well. Here’s the basic format:
interval="[Daily|Weekly|Monthly|Interval Value in Seconds]"
startTime="#TimeFormat(Now(), "hh:mm:ss tt")#'
This piece of code create the task “MyScheduledTask” if it does not exist and updates it if it does. The task attribute specifies the name of the task, the interval is how often it will run. (You can also specify an endDate and endTime so that it will stop running after a certain date and time.) The operation Attribute specifies that this is an HTTP request to another URL. Then you specify the URL you want to use.
Sending an HTTP request to example.com will not really do anything. We could, though, choose to save the results of the HTTP request by specifying values for the publish, path and file attributes. (Specify the full path sans the file name for the path attribute and the file name in the file attribute… Obviously.) Make sure that publish is equal to “Yes”. Then after the task runs, the results of the task will be written to that file.
You can also choose to delete and run tasks. If you want to use one of these, specify either “delete” or “run” in the action and the name of the task in the “task” attribute.
There are a few other options for this tag as well, which can be viewed in the LiveDocs. You can basically do anything from this tag that you can do in Administrator. (If you don’t have access to the Administrator, you could use this tag in combination with a database and build your own task administrator.)
Note that some hosts do not allow scheduled tasks and I think that some even disable the CFSCHEDULE tag altogether, so check with your host before trying to use that.