rss
twitter
    Find out what I'm doing, Follow Me :)

Sunday, April 11, 2010

Pin It

Widgets

Magento 1.4 cronjobs

On some points, Magento needs regular maintenance. For instance, when running a shop of which the products are updated frequently, it is needed to refresh catalog indices. Also the search-index might need regular updating. To accomplish these tasks you can run Magento cronjobs. With MagentoTM 1.4 this is even easier. Here's how.

Meet cron

Within Linux environments, the program responsible for running scheduled tasks is called cron. Tasks that are run through cron are referred to as cronjobs. By runnning cronjobs daily or perhaps weekly, you can automate certain processes like refreshing the Magento indices or cleaning up logfiles.

Running cronjobs is something that depends on your hosting environment. Almost every Linux-server is shipped with cron-functionality, but within shared hosting environments it just depends whether you are allowed to use or not. Popular control panels like DirectAdmin or CPanel often allow you to create new cronjobs.

Running a Magento 1.4 job through cron

With Magento 1.3 and older, you had to write your own PHP-scripts to get things done. With Magento 1.4 this has become much easier. Within the Magento directory "shell" you can find various PHP-scripts that automate certain actions. The rest of this tutorial will discuss those actions as well.

Running such a script through cron doesn't mean calling this script through the browser. You'll need command-line access to your Magento site to run such a script manually. In exactly the same way that you run this from the command-line, the cronjob is also run. In the following examples, you will need to replace "/path/to/magento" with the exact directory that contains Magento.

php /path/to/magento/shell/indexer.php reindexall

This will reindex all the Magento indices. Now this command first calls the PHP-program "php" with as first argument the location of the PHP-script, and as second argument something that is used as argument for the script.

It might be that the "php" program is not found by cron, in which case you will need to define the exact path to the "php" binary program:

/usr/local/bin/php /path/to/magento/shell/indexer.php reindexall

To surpress output from the Magento script you can also add the flag "-q" (quiet):

php -q /path/to/magento/shell/indexer.php reindexall

More Magento reindexing

When running the indexer.php script from the command-line we can find more tricks. First of all we can check for the current status of all the indices:

php /path/to/magento/shell/indexer.php --status

We can also refresh a specific index. To do so, we first need to find out which arguments can be used:

php /path/to/magento/shell/indexer.php info

This gives a listing like the following:

catalog_product_attribute     Product Attributes
catalog_product_price         Product Prices
catalog_url                   Catalog Url Rewrites
catalog_product_flat          Product Flat Data
catalog_category_flat         Category Flat Data
catalog_category_product      Category Products
catalogsearch_fulltext        Catalog Search Index
cataloginventory_stock        Stock status

Now we found the technical names for the various indices, which we can use like the following example:

php /path/to/magento/shell/indexer.php --reindex catalog_product_flat

The last example is again something we can use through cron.
Clean logs and using the Magento compiler

Besides refreshing indices, we can also clean up all the logs within the database:

php /path/to/magento/shell/log.php clean

Even cooler is the new Magento Compiler module which is still in beta but gives great performance benefit by reducing the number of include-paths (4 by default) to one single include-path. This mainly saves CPU-resources.

You can check for the current state of the compiler by running the following:

php /path/to/magento/shell/compiler.php state

Once checked you can disable the compiler temporarily, rebuild the include-paths again with the flag "compile" and enable the compiler once more.

php /path/to/magento/shell/compiler.php disable
php /path/to/magento/shell/compiler.php compile
php /path/to/magento/shell/compiler.php enable

4 comments:

  1. hi,

    I have upgraded my Magento from 1.3.x to 1.4.0.1 and now my payment module (which is using Paypal Standard) is not working and always showing processing or pending and giving an error while checking the order in frontend or backend (having an error of Configuration can't be load)

    Please help

    ReplyDelete
  2. Thanks for the great article, really well laid out and easy to follow.

    The first thing I found when trying the indexer was an error about APC memory, the cure for that was to add:
    apc.enable_cli=1
    to the php.ini file.

    Anyone know of a way to run advanced import profiles via cron?

    ReplyDelete
  3. Great post! Running the catalog indexing via shell is so much faster.

    thanks!

    ReplyDelete
  4. Awesome post. Thank you so much for sharing.

    ReplyDelete