SpinOneSolutions | I waste my time so that you don’t have to…

Dec/10

15

Magento config.xml Cron Job

It’s possible to setup process to run on a schedule provided that your server is setup to run cron. All internal cron jobs are run from a single page/script call. You’ll need to setup your server to either; make a request to /cron.php or execute /cron.sh.

The time delta that you set here will be the minimum delta for your Magento jobs.  IE: If your server’s cron  requests /cron.php every ten minutes and you create a cron job within Magento to run every five minutes, you shouldn’t be surprised to see it running every ten minutes instead of every five.

This won’t be a huge deal for all of you who are administering your own box, but for those of you on a shared box; you’ll likely be limited to a certain minimum delta which you’ll want to be aware of.

Setting up a cron job is actually really simple so lets take a look.  The configuration lives in config.xml, which shouldn’t be a surprise for you module developers.  Using my default example module Spinonesolutions_Helloworld:

/app/code/local/Spinonesolutions/Helloworld/etc/config.xml (partial)

<config>
...
<crontab>
<jobs>
<spinonesolutions_helloworld>
<schedule><cron_expr>*/10 * * * *</cron_expr></schedule>
<run><model>spinonesolutions_helloworld/observer::foobar</model></run>
</spinonesolutions_helloworld>
</jobs>
</crontab>
...
</config>

You can do a Google search on cron schedules in order to figure that part out, it’s outside the scope of this article.  The only other piece that deserves explanation is the static method call between the run tag.  As you’d expect my example call is referencing Spinonesolutions_Helloworld_Model_Observer (/app/code/local/Spinonesolutions/Helloworld/Model/Observer.php). This file doesn’t need to contain anything except for a class definition and a static method.

/app/code/local/Spinonesolutions/Helloworld/Observer.php

<?php
class Spinonesolutions_Helloworld_Model_Observer {
  public static function foobar() {
  }
}

That’s it.  Your static method will now be called on the schedule that you set!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Kirtsy
  • Propeller
  • Reddit
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati

RSS Feed

6 Comments for Magento config.xml Cron Job

Michael | February 7, 2011 at 3:27 pm

IIRC, that should be “public static function foobar()”, otherwise it’s not a static method and will require that your Observer class be instantiated first.

Will Wright | February 7, 2011 at 3:42 pm

Too true! Fixed, and thank you!

Chassidy Bellaire | March 9, 2011 at 10:02 am

Great beat ! I would like to apprentice while you amend your web site, how can i subscribe for a blog site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea

dwilmer | May 11, 2011 at 4:19 am

Now that your function is static, returning $this will either fail or produce unexpected results. Either way, you don’t want to return $this in a static function.

Will Wright | May 16, 2011 at 8:59 am

Too True! I almost always return “$this” and it’s a force of habit when I create shell functions to return it.

Ivn | November 28, 2011 at 4:01 am

Hi there,

I need to create a cron job in Magento which should send emails to customers whose order status is complete. I need to schedule this cron job such that it checks for order with status = “complete” and send them emails. Also the customers which are already sent an email should be marked so that they are not sent email twice.

I have created a custom module and in its config.xml I have added my template email and cron job scheduled.

Also in the Module>>Model>>Observer.php I have declared my sendEmail() trying to send a test email to my email id.

I try to run this cron task by running http://yourdomain.com/cron.php. But I fail to get any mail.

Leave a comment!

«

»

Find it!

Theme Design by devolux.org