Creating cron script in Magento
5 Comments 13th JAN 2010 | Posted by Domagoj Potkoc in Magento

Creating Magento cron script is very simple thing. First of all we have to create the module, (I hope that you know how to create magento module) and add in config.xml file next code:
<crontab>
<jobs>
<inchoo_birthday_send>
<schedule><cron_expr>0 1 * * *</cron_expr></schedule>
<run><model>birthday/observer::sendBirthayEmail</model></run>
</inchoo_birthday_send>
</jobs>
</crontab>
Next step, we have to create model file observer.php (in folder model) with method sendBirthayEmail
class Inchoo_Birthday_Model_Observer
{
public function sendBirthayEmail()
{
//this collection get all users which have birthday on today
$customer = Mage::getModel("customer/customer")->getCollection();
$customer->addFieldToFilter('dob', array('like' => '%'.date("m").'-'.date("d").' 00:00:00'));
$customer->addNameToSelect();
$items = $customer->getItems();
foreach($items as $item)
{
// send email or do something
}
return $this;
}
}
This cron will be executed every day at 01:00 AM. The important thing: You have to set cron execution on the server (www.yourstore.com/cron.php), without it will not work.
To post code in comments, place your code inside [code] and [/code] tags.


















January 19th, 2010 at 11:41
can you provide me complete module for this
February 2nd, 2010 at 20:26
Hi, I posted a comment on Toni Anicic’s blog about cron.
http://inchoo.net/ecommerce/magento/magento-newsletter/
He’s been very kind and tried to help, but maybe you are the right person for this topic.
We are experiencing strange behavior with cron-based.
After the two comments in Toni’s blog, we re-queued the newsletter, launched cron via wget and are observing the mail server’s behaviour.
Current batch size ($countOfSubscritions) is 20.
Right after the cron run, the mail server received 105 e-mails for delivery.
How’s it that 20 becomes 105 with a single cron run?
April 7th, 2010 at 12:12
“This cron will be executed every day at 01:00 AM. The important thing: You have to set cron execution on the server (www.yourstore.com/cron.php), without it will not work.”
What is role of crontab command on linux(CentOS 5.3)
and What is role of “cron.php” ?
and What is role of magento’s cron script ?
Have any relevant above three kind of task ?? please….
June 28th, 2010 at 15:09
Great tip, just another question? How allow the store’s administrator to choose when execute the script?
September 1st, 2010 at 10:27
I did EXACTLY what you’ve suggested but it doesn’t work – in cron_schedule table in magento’s base I’m getting following error: ‘exception ‘Mage_Core_Exception’ with message ‘Invalid callback: birthday/observer::sendBirthayEmail does not exist’….’. do you have any idea why?