Skip to main content
October 30, 2009
Question

Run a CF program as a background process?

  • October 30, 2009
  • 3 replies
  • 1909 views

I want to write a ColdFusion program that sends an email to a list of people whenever a particular column in a database is edited. I know how to do this, but I want it to run automatically everyday, rather than me having to run it myself. Is there a way to do this??

    This topic has been closed for replies.

    3 replies

    Inspiring
    October 30, 2009

    Scheduled tasks are a great tool in CF but I wanted to clarify something in your post. Do you want this script (program) to run automatically each day at a set time or do you want it to run only when the DB is updated?

    If you just want it to run at a scheduled time, follow Ian and Adam's advice and check into the CF Docs for Scheduled Tasks. They're easy to setup and use ... and quite helpful in so many cases.

    If you're looking to have this script run only after your DB column is updated, you may want to take an alternate approach. And there are quite a few options you have available.

    A very simple approach would be to just add a CFMAIL tag after you run the update. This assumes that you have some ColdFusion code that processes the update. If this is the case, you could simply add a CFMAIL tag after your update code executes.

    <cfquery name="qry" datasource="#dsn#">

         update mytable

         set col1 = 20

         where id = #id#

    </cfquery>

    <cfmail to="mypeeps@email.com" from="me@email.com" subject="data">

         <!--- email details --->

    </cfmail>

    Another option would be to have your DB do the work for you, depending on your DB server that is. You could setup a trigger in your DB (I know MySQL and MS SQL support triggers and email).

    Basically, the trigger listens for changes on a specified table and when it detects you have updated a column in the specified table, it will run its code, including sending the email directly from your DB. I only mention this because I just learned how to do it for MySQL and MS SQL and it's pretty fun and cool ... well, 'cool' should probably be in quotes !

    October 30, 2009

    I hadn't yet decided whether I wanted the program to run when the db is edited or at a certain time each day - for what I'm doing either would work, so I'll look into both of those suggestions and see which seems neater. Thanks guys, responses are really helpful :-)

    Inspiring
    October 30, 2009

    Have a skim through the docs for scheduled tasks.

    --

    Adam

    ilssac
    Inspiring
    October 30, 2009

    Well there is the feature in ColdFusion called "Scheduled Tasks".

    With this feature, either through the ColdFusion Administrator or with the relativily new <cfscheduled...> tag, you can schedule a URL to be requested on a regular basis.