Creating cron script 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:
0 1 * * *
birthday/observer::sendBirthayEmail
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.
Published in:
Leave a comment
27 comments
My cron job for changing customer attribute not working.
.Same code works when I copy that in a controller action and hit by url.
.On AOE_Scheduler extension, it shows processing and never change from that.
.I also put this code in try catch but no exception generated.
$user = Mage::getModel(‘customer/customer’)->load($customer->getEntityId());
$firstname = ‘Saeed’; $lastname = ‘Khan’;
$user->setFirstname($firstname);
$user->setLastname ($lastname);
$user->save();
Any suggestion will be appreciated.
This helped me with my job today, thanks.
I have CentOS on my server. I have written your code properly but still my cron job is not running.
My cron.php is running but can not see the code getting executed
how can i config a custom day in admin to run cron?
hi,
I have same problem as KOB.
I try to send an email template via cron.
I know the cron function works b’coz I can send emails hard coded like : mail(“mymail@example.com”,’bla’,’bla’);
and I know that the code I use for sending the email template works b’coz when I put the code in a .phtml file and run it through the browser it sends the email.
It is only when I try to use the template inside the cron function that it is not working.
any ideas what can I do?
Please do help
Thanks Inchoo for good stuff
1.0.1
Demo_Practice_Model
*/5 * * * *
practice/observer::logmethod
*/5 * * * *
practice/observer::method1
I found good module zip here For module zip visit http://blogswindow.com/magento-cron-job-setup-using-module
I used your tutorial and my cron job is working fine.
But i have one query related to cron job. Can i create a cron job for controller method if yes how?
Hello sir ,
How can i execute custom URL of magento files by cron job through cron job setting with magento admin panel .
There’s a typo in model code –
… public function sendBirthayEmail() …
missed “d” in “Birthay” that’s maybe a reason of exceptions
Dear Magento Developers,
I’ve created a module with cron job working and I give it free to those that ask me through email from my blog (when the new blog will be online you can download it from there).
Now you’ve the chance to test it with no issues.
Thanks
Regards
Dear Domagoj Potkoc,
Thanks for the tip but your post is not complete.
By saying “I hope that you know how to create magento module” you miss very important information.
For example, my module doesn’t work because probably i miss to declare the
in the config.xml.
Is the purpose of this post to contact you for premium services only?
You help is appreciated.
Thanks
Regards
we have wamp server in window 7 how can i execute a module in crone
is there any way we can run cron by direct run url in browser.
HI.
I try to send an email template via cron.
I know the cron function works b’coz I can send emails hard coded like : mail(“mymail@example.com”,’bla’,’bla’);
and I know that the code I use for sending the email template works b’coz when I put the code in a .phtml file and run it through the browser it sends the email.
It is only when I try to use the template inside the cron function that it is not working.
any ideas what can I do?
@Gz Chauhan
I think if you need to run your cron every 12 hours, may be this expression should work.
* 0,12 * * *
Try it out!
Hi..
i create cron module and it work fine but problem is that when i set
0 */12 * * *
in config.xml it not send mail every 12 hours.. can u say this is right expresion or not.. can u give me right expresion for every 12 hours cron set
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.
Any suggestions please?
Sorry, my first comment was errorous…
It is OK. The global section is important :))))
Don’t forget to add the model in the config.xml file’s global section!
With using the package ang module name of the example:
It will eliminate the ‘invalid callback’ error mentioned above by Codep and cron will work.
If you want to run it on Centos or other OS. You can get info from http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job#cron.sh or http://fishpig.co.uk/magento-cron-jobs/ and .
I really want the functioin ! That ten day after I send the products to customer , then system send a email to customer !? any idea?
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?
Great tip, just another question? How allow the store’s administrator to choose when execute the script?
“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….
Hi, I posted a comment on Toni Anicic’s blog about cron.
https://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?
can you provide me complete module for this