Knowledge Base/Setup & Customization/System Requirements

Setting up CRON Jobs in Magento

Sherrie Rohde
posted this on March 23, 2012 14:10

Cron jobs, or scheduled tasks, must be enabled in order for Magento to operate properly. Since Sweet Tooth is built upon Magento's functionality, it also inherits this requirement.

By default if you're running a UNIX operating system it should automatically recognize Magento's requests to use the CRON TAB service. Magento needs Cron Jobs to be initiated for the following features to operate correctly:

  • Catalog Price rules
  • Sending Newsletters
  • Generating Google Sitemaps
  • Customer Alerts/Notifications (product price change, product back to stock)
  • Automatic updating of currency rates
  • Scheduled DB logs cleanup

Evidence of Magento not picking up CRON TAB tasks properly in Sweet Tooth is if the catalog redemption point rules block disappears one day after you applied all rules. If this is the case please consider the following content.

(For additional information on Cron Jobs in Magento, check out the Magento Wiki article.)

Check to see if Cron is Running using Test Sweet

Sweet Tooth has built in diagnostic tools available that will let you know whether or not your Magento Cron is set up properly. To run this tool, navigate to Rewards > Configuration > Other Configuration and scroll down to Diagnostics & Support Tools.

DiagnosticsAndSupportTools.png

To run Test Sweet, click on the Run 'Test Sweet' Diagnostics button.

RunTestSweet.png

Next you'll see several lines of diagnostics. Scroll down until you see Magento - Cron.

If the Cron is setup and running properly, you'll see the following:

MagentoCron-Pass.png

If the Cron is not setup properly, Test Sweet will show you this warning:

MagentoCron-Fail.png

Follow the directions below to setup your cron.

Setting up the Cron In a UNIX Based Operating System

You can use the crontab service command if you have shell access to the server, or add it through your cPanel or similar admin. You need to make the script run every 5 minutes, so in advanced configurations you'd be entering in:

*/5 * * * *

The your crontab should have something like one of these:

*/5 * * * * /bin/sh /[magento_path]/cron.sh
*/5 * * * * /bin/bash /[magento_path]/cron.sh
*/5 * * * * /usr/bin/php /[magento_path]/cron.php
*/5 * * * * /usr/local/bin/php -f /[magento_path]/cron.php

Setting Up the Cron Using cPanel Version 11

If you're using cPanel 11, you can setup a Cron Job using the advanced tab.

  1. Login to cPanel and locate the Advanced panel as shown here:

    Screen_Shot_2012-06-21_at_8.53.14_PM.png
  2. Click on the Cron Jobs icon.

    CronJobs.png
  3. Enter the following settings in the Add New Cron Job section, replacing [magento_path] with the path to your Magento install:

    Screen_Shot_2012-06-21_at_8.57.33_PM.png
  4. Click Add New Cron Job to insert this cron job in the system. 

Setting up a Scheduled Task In Windows

In windows you need to create a scheduled task that will execute the cron.php file every 5 minutes. To do this, go to the scheduled task manager and add a task to run the folllowing command:

php C:\[magento_path]\cron.php

Additional Testing to see if CRON is running

You can place some probes into cron.sh and cron.php to confirm things are running.

You can add this to cron.sh after the INSTALLDIR variable is set:

# absolute path to magento installation
INSTALLDIR=`echo $0 | sed 's/cron\.sh//g'`
date >> "$0".log
whoami  >> "$0".log

And this to cron.php after the dispatchEvent call:

Mage::dispatchEvent('default');
$log = fopen(__FILE__.'.log', 'a');
fwrite($log, date("Y-m-d H:i:s").PHP_EOL);
fclose($log);

After 6 minutes you should see both these files in Magento's directory if cron is running.

./cron.php.log
./cron.sh.log

If you have tried everything and still cant get cron to work, perhaps this unsupported workaround might help. Adding the following to the top of index.php will cause cron to run in the background whenever someone accesses your website.

<?php
$pathToCron = dirname(__FILE__)."/cron.sh";
exec( "sh $pathToCron > /dev/null &" );

Another workaround

*/5 * * * * /usr/bin/wget http://your_webpage.com/cron.php
 

Comments

User photo
Bhorselman

For those using the cpanel option:

The '[magento_path]' will be the server path.

ie. /home/'yourusername'/public_html/cron.php

December 30, 2012 18:02