1

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.

  • 1
    I assume it is because there is no cron_expr in your config.xml and when cron.php is run there is no store so there is no core_config_data, maybe have a look at Mage_Cron_Model_Schedule::setCronExpr – David Manners May 22 '14 at 7:32
  • @DavidManners, Yes I don't have cron_expr in my config.xml Because I have stored in core_config_data. – Manoj Kumar May 22 '14 at 7:49
  • do you call the cron.sh via linux cron regularly? – Fabian Blechschmidt May 22 '14 at 8:26
  • @FabianBlechschmidt, No, I'm using windows. Can you tell me how can call cron.sh via windows. – Manoj Kumar May 22 '14 at 8:51
  • Manoj I have no idea. But without external call to the magento cron there will no cronjob run. What you can do is just call php cron.php – Fabian Blechschmidt May 22 '14 at 9:05
3

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.

  • how can i run `cron.php'? can u suggest me. – Manoj Kumar May 22 '14 at 8:52
  • 1
    in your server's /etc/crontab add the line suggested by @sebastian */5 * * * * php -f /path/to/magento/cron.php – Julien Lachal May 23 '14 at 12:16
3

As mentioned by Julien you have to run cron.php in root directory. With

crontab -e

in terminal you can add the cronjob to the server. This is the entry you have to make:

*/5 * * * * php -f /path/to/magento/cron.php

To list all cronjobs you can use

crontab -l

I read to late that your magento is running on windows. On windows you use the task scheduler to run cron.php

2

I strongly recommend AOE Scheduler extension to see what's going on with cron. Among other things it shows if Magento has "heartbeat". If not, then it probably means that there's something amiss with crontab: either it doesn't run cron.php, insufficent permissions, you try to run it directly without interpreter or your path is wrong.

You can find the extension here:

http://www.magentocommerce.com/magento-connect/aoe-scheduler.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.