Magento – Cron jobs are not working

cronmagento-1.8php-5.4

I was created the Cron jobs it is showing on admin panel and I have set the time also, but it is not running.

my code is

system.xml

<?xml version="1.0"?>

<config>
    <tabs>
        <Auto translate="label">
            <label>Auto Replenish</label>
            <sort_order>120</sort_order>
        </Auto>
    </tabs>
    <sections>
        <autoreplenish translate="label" module="autoreplenish">
            <label>Auto Replenish Settings</label>
            <tab>Auto</tab>
            <frontend_type>text</frontend_type>
            <sort_order>1</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <general_settings translate="label">
                    <label>Enable Auto Replenish</label>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>0</show_in_website>
                    <show_in_store>0</show_in_store>
                    <fields>
                        <enable_disable translate="label, comment">
                            <label>Enable</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </enable_disable>
                    </fields>
                </general_settings>
            </groups>
        </autoreplenish>
        <catalog>
            <groups>
                <configurable_cron translate="label">
                    <label>Cron Schedule</label>
                    <sort_order>100</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>0</show_in_website>
                    <show_in_store>0</show_in_store>
                    <fields>
                        <time translate="label">
                            <label>Start Time</label>
                            <frontend_type>time</frontend_type>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </time>
                        <frequency translate="label">
                            <label>Frequency</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_cron_frequency</source_model>
                            <backend_model>autoreplenish/adminhtml_system_config_backend_autoreplenish_cron</backend_model>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </frequency>
                    </fields>
                </configurable_cron>
            </groups>
        </catalog>
    </sections>
</config>

cron.php

<?php

class Easylife_Autoreplenish_Model_Adminhtml_System_Config_Backend_Autoreplenish_Cron extends Mage_Core_Model_Config_Data
{
    const CRON_STRING_PATH = 'crontab/jobs/my_cron/schedule/cron_expr';
    protected function _afterSave()
    {
        $time = $this->getData('groups/configurable_cron/fields/time/value');
        $frequencyDaily = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_DAILY;
        $frequencyWeekly = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_WEEKLY;
        $frequencyMonthly = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_MONTHLY;
        $cronDayOfWeek = date('N');
        Mage::log(date('N'));
        $cronExprArray = array(
            intval($time[1]),                                   # Minute
            intval($time[0]),                                   # Hour
            (frequency == $frequencyMonthly) ? '1' : '*',       # Day of the Month
            '*',                                                # Month of the Year
            (frequency == $frequencyWeekly) ? '1' : '*',        # Day of the Week
        );
        $cronExprString = join(' ', $cronExprArray);
        Mage::log($cronExprArray);
        try {
            Mage::getModel('core/config_data')
                ->load(self::CRON_STRING_PATH, 'path')
                ->setValue($cronExprString)
                ->setPath(self::CRON_STRING_PATH)
                ->save();
        }
        catch (Exception $e) {
            throw new Exception(Mage::helper('cron')->__('Unable to save the cron expression.'));
        }
    }
}

Replenish.php

<?php


class Easylife_Autoreplenish_Model_Replenish
{
    public function run()
    {
        Mage::log("Cron Jobs");
        return true;
    }
}

config.xml

<crontab>

        <jobs>
            <my_cron>
                <run>
                    <model>autoreplenish/replenish::run</model>
                </run>
            </my_cron>

        </jobs>
    </crontab>

I am save the CST time @ 11:38:00 and it's saved on core_config_data as catalog/configurable_cron/time = 11,38,00 & catalog/configurable_cron/frequency = D & crontab/jobs/my_cron/schedule/cron_expr = 38 11 * * *. But it's not saved on cron_schedule.

Can you tell me where I went wrong?

Thanks in advance.

Best Answer

Please make sure you run Magento's cron.php (located in Magento root folder) every 5 minutes or so (in your server's crontab). Doing so, Magento will check all the XML files for your modules and check for the nodes. Then it will add jobs to the queue in table cron_schedule (in your DB). After a short moment, you shoudl see this table populating. Magento will launch the crons by itself.

Hope this helped.

Related Topic