How to setup a cron job in Magento module?
In your modules config.xml
put the following:
<config>
<global>
<models>
<roomstoryinvoicecron>
<class>Roomstory_Invoice_Model</class>
</roomstoryinvoicecron>
</models>
</global>
<crontab>
<jobs>
<roomstoryinvoicecron>
<schedule>
<cron_expr>*/10 * * * *</cron_expr>
</schedule>
<run>
<model>roomstoryinvoicecron/observer::setStatus</model>
</run>
</roomstoryinvoicecron>
</jobs>
</crontab>
</config>
In app/code/local/Roomstory/Invoice/Model/Observer.php
add the following:
<?php
class Roomstory_Invoice_Model_Observer {
public function setStatus() {
Mage::log("WORKS!");
}
}
Make sure logging is enabled and it should work, check the log to be sure ;)
How to setup cron dynamically via admin in Magento 2
Magento2 has a different scheme to merge layout config so you have to create a new file that called crontab.xml under your_custom_module/etc folder. And then you can add your cron config like this one:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="custom_cronjob" instance="YourVenDoerName\CustomModule\Cron\Test" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
How to setup magento cron job and init action of the controller?
As far as I have ever seen, you cannot directly run controller actions in a cronjob, since that is not a terribly sensible thing to do outside of a user context. The easiest thing to do here is to put that functionality you need into a model and call that from the cronjob.
More information on the controller action you want to fire would be helpful in giving further advice.
Hope that helps!
Thanks,
Joe
Related Topics
Sql_Calc_Found_Rows/Found_Rows() Does Not Work in PHP
Secure and Flexible Cross-Domain Sessions
Saving Habtm with Extra Fields
Join Subquery with Doctrine 2 Dbal
Tablegateway with Multiple from Tables
PHP Hsv to Rgb Formula Comprehension
Symfony2: How to Access Service from Template
Mailgun Sent Mail with Attachment
Customize Addresses Fields on Woocommerce My Account and Checkout
Using SQLite3 in PHP How to Count the Number of Rows in a Result Set
How to Avoid Resending Data on Refresh in PHP
Error 405 (Method Not Allowed) Laravel 5
How to Get MySQL 8 to Run with Laravel
Formatting an SQL Timestamp with PHP