Set Up Cron Jobs In Magento

Hi audiences, we are back with another article about intersting and useful technique in Magento that you will find it extremely necessary now and then to run some command automatically, ie. How to set up cron jobs in Magento. All Magento seniors agree that cron jobs do help them extensively in terms of time and resource savings. As a beginner, you are fortified to enhance your ability to set up and run your first cron job following our tips shared below. Note that cron only works if the server which your Magento hosts  is already setup for cronjobs.

There are two options to setup cron jobs for a Magento server:

1. Use cPanel to setup cronjobs

Log onto cPanel, move to Cron jobs:

img-1Select the parameters to set up a new Cron Job:

img-2You are able to either choose quick setup in the Common Settings or select individual options for Minute, Hour, Day, Month and Weekday.Note that:

Minute: how frequency in minutes per running time? Or at which minute of the hour it runs?

Hour: how frequency in hours per running time? Or at which hour?

Day: how frequency in days per running time? Or on which days of the month?

Month: how frequency in months per running time? Or in which months of the year ?

Weekday: On which days of the week?

Command: php -f /absolute_path_of_your_magento_root_folder/cron.php

Eg.php -f /home/abc/public_html/cron.php

After you have selected the options, click  “Add New Cron Job”

2. Use ssh to setup Cron jobs:

Log onto ssh account. To check if your server has Magento cronjob set up, run this command:

crontab –l

If not, you need to open crontab file to set it up:

crontab –e

Use “PgDn” on the keyboard to move to the final line and press  “Enter” to create a new line.

Input this line:

*/5 * * * * wget -O /dev/null -q http://www.yourdomain.com/cron.php > /dev/null

Note: */5 * * * *is the parameter to run the cronjob, in this case the cronjob is set up to run automatically every 5 minute every day, http://www.yourdomain.com/is your Magento website URL.

Press “Ctrl+X” and then press “Enter” to save the cronjob.

After you have established cronjob for the server, you are now ready to set up cronjob for Magento modules.  There are also 2 solutions to set up cronjob for modules, namely fixed and flexible time. For the fixed time choice it means you can set up a specific timeframe to run cronjob in your config.xml file, whereas the 2nd choice allows admin to adjust timeframe of the cronjob in backend panel.

  1. Fixed time:

You will set the time for cronjob in:

app/code/local/Name_Space/Module_Name/etc/config.xml

In between the tab:

<config>

….

</config>

Add the following code:

<crontab>

<jobs>

<modulename_cron_name>

<schedule>

<cron_expr>*/5 * * * *</cron_expr>

</schedule>

<run>

<model>modulename/observer::mothodName</model>

</run>

</ modulename_cron_name>

</jobs>

</crontab>

Note:

modulename_cron_name: you give it a name

*/5 * * * *: is the parameter to run the cronjob, in this case the cronjob is set up to run automatically every 5 minute every day.

<model>modulename/observer::mothodName</model>: modulename is the name of the module, in folder Model, create a file named Observer.php as follows:

<?php

class Namespace_Modulename_Model_Observer

{

public function mothodName(){

your process…

}

}

  1. Create a function to set up flexible time for a cronjob in backend panel:

Firstly, you need to add the following code to your config.xml file like you did in the first solution:

<crontab>

<jobs>

<modulename_cron_name>

<run>

<model>modulename/observer::mothodName</model>

</run>

</ modulename_cron_name>

</jobs>

</crontab>

And create Observer.php file like mentioned above, however, the difference of this 2nd solution lies in the fact that you don’t need to set up timeframe for the cronjob in config.xml file.

In order for admin to set time for a cronjob, you need to create a general configuration in system.xml file.

Go to: app/code/local/Name_Space/Module_Name/etc/system.xml

Add the following code to create a new tab for the module configuration board:

<crontab_setting translate=”label”>

<label>Crontab settings</label>

                <frontend_type>text</frontend_type>

<sort_order>900</sort_order>

                <show_in_default>1</show_in_default>

                <show_in_website>1</show_in_website>

                <show_in_store>1</show_in_store>

                <fields>

<frequency translate=”label”>

                                <label>Frequency</label>

                                <frontend_type>select</frontend_type>

                                <source_model>

adminhtml/system_config_source_cron_frequency

</source_model>

                                <backend_model>

modulename/system_config_source_cron

</backend_model>

                                <sort_order>4</sort_order>

                                <show_in_default>1</show_in_default>

                                <show_in_website>1</show_in_website>

                                <show_in_store>1</show_in_store>

                </frequency>

                <time translate=”label”>

                                <label>Start Time</label>

                                <frontend_type>time</frontend_type>

                                <sort_order>3</sort_order>

                                <show_in_default>1</show_in_default>

                                <show_in_website>1</show_in_website>

                                <show_in_store>1</show_in_store>

                </time>

</fields>

</crontab_setting>

In folderModel, create a Cron.phpfile following this path:

System/Config/Source/Cron.php

Containing:

<?php

class Namespace_Modulename_Model_System_Config_Source_Cron extends Mage_Core_Model_Config_Data

{

const CRON_STRING_PATH = ‘crontab/jobs/modulename_cron_name/schedule/cron_expr’;

const CRON_MODEL_PATH = ‘crontab/jobs/modulename_cron_name/run/model’;

protected function _afterSave()

{

$time = $this->getData(‘groups/ crontab_setting/fields/time/value’);

$frequncy = $this->getData(‘groups/ crontab_setting/fields/frequency/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’);

$cronExprArray = array(

intval($time[1]),

intval($time[0]),

($frequncy == $frequencyMonthly) ? ‘1’ : ‘*’,

‘*’,

($frequncy == $frequencyWeekly) ? ‘1’ : ‘*’,

);

$cronExprString = join(‘ ‘, $cronExprArray);

try {

Mage::getModel(‘core/config_data’)

->load(self::CRON_STRING_PATH, ‘path’)

->setValue($cronExprString)

->setPath(self::CRON_STRING_PATH)

->save();

Mage::getModel(‘core/config_data’)

->load(self::CRON_MODEL_PATH, ‘path’)

->setValue((string) Mage::getConfig()->getNode(self::CRON_MODEL_PATH))

->setPath(self::CRON_MODEL_PATH)

->save();

} catch (Exception $e) {

throw new Exception(Mage::helper(‘cron’)->__(‘Unable to save the cron expression.’));

}

}

}

These are the two tips for Magento developers to set up cronjob for Magento server and modules. Whether you are a beginner or an expertise, we truly hope this article finds you well. All your comments and contributions to our blog are highly appreciated. Thanks for your time.

15979 Total Views 3 Views Today