Scheduling Tasks Without Access to CF Administrator

…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:


task="MyScheduledTask"
interval="[Daily|Weekly|Monthly|Interval Value in Seconds]"
startDate="#DateFormat(Now(), "mm/dd/yyyy")#"
startTime="#TimeFormat(Now(), "hh:mm:ss tt")#'
operation="HTTPRequest">
url="http://www.example.com"

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.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Megan

    Thanks! David – that was great :) ~megan

  • http://www.delyrical.com davidjmedlock

    Glad you liked it Megan. You should register for the Sitepoint Forums so that it’ll be easier to “converse” with you. It’s a pretty cool place to just hang out… I’ll eventually work through your whole list. Just need a little time. :)

  • RadhikaKiran

    This article prooved to be of great help to me as I din’t know where to start and this article gave me a direction. Thanks David

  • derek

    Thanks so much for this information. Obviously my host doesn’t give us access–I’ll play around to try to get this tag working with my particular use.

  • William

    Execution of cfschedule can be disabled in the Administrator.

  • Mali-Bee

    Can’t see the schedule tag code. Was able to see it when I did view source though. BTW – in your current example, the url attribute is not included in the tag, but should be.