Executing cron tasks manually in Magento

Usually I’ve been asked about how to run a cron task manually. A cron task defined in Magento is not anything else than a function that belongs to a model that is loaded and executed.

For example, we have this cron task on a config.xml file of a specific module:

<catgento_sap_sync_nonimages>
   <schedule>
       <cron_expr>*/15 * * * *</cron_expr>
   </schedule>
   <run>
       <model>sap/cron_sync_nonimages::run</model>
   </run>
</catgento_sap_sync_nonimages>

It’s easy to identify the model and function that is being loaded and executed as it is inside the <model> tags. The model that is being loaded is sap/cron_sync_nonimage, and the function inside that model that is being executed is run.

Knowing this, we can create a new script that loads and executes that cron task like this (create a script.php file and put something like this inside):

<?php

//Load Magento API
require_once 'app/Mage.php';
Mage::app();

//First we load the model
$model = Mage::getModel('sap/cron_sync_nonimage');

//Then execute the task
$model->run();

The last step would be running this script manually (php script.php).

3 comments

    • jordan314 on November 12, 2014 at 1:47 am

    Reply

    Thanks for this! This worked perfectly.

    • bejoy on June 15, 2017 at 6:17 am

    Reply

    How can we log these in cron_schedule table? success or failures etc…

      • Pau on June 28, 2017 at 5:16 am
      • Author

      Reply

      Hi Bejoy,

      Actually, this won’t get logged in the cron_schedule table, because you’re not executing it through the normal way. If you want to log this, either you add your log lines in your class, either you try to find out how the cron class executes it.

Leave a Reply

Your email address will not be published.