June 25, 2011

Magento setup cronjob for google sitemap and other tasks using SSH on unix

This post may be not for everybody, because it crosses some speciality lines. But there are many multidisciplinary admin/programmers out there doing this daily because some tasks just have to get done.

First we are going to configure the Google Sitemap in Magente Admin and then we are going to log onto a Unix system via SSH and edit the crontab. And while we are at it, we are going to configure some additional, but important cronjobs. A cronjob is something like a scheduled task. The computer will execute that task at exactly the time or date you have configured. It is an automated process that will take routine tasks out of your hands.

Configuring Google Sitemap in Magento

First setup the cronjob in Magento:
In Magento Admin > System > Configuration > Catalog > Settings and configure:

  1. Enable
  2. Fill in the time to run the Cronjob. I prefer after midnight.
  3. Choose a frequency. Daily is best.
  4. Fill in a TO address. In case the Cronjob doesn't go well an error report will be sent..
  5. Fill in a  FROM address for that mail.
  6. Choose a Email Template which will be used for the error report.
Now we setup the sitemap. I will keep in mind a sitemap setup for multiple stores.
  1. Add sitemap
  2. Filename HAS to be sitemap.xml. No variations are possible.
  3. Path. Last path folder has to be writable (777). I have used /sitemap/shopa/. As you can see, within the /sitemap/ folder, each Shop will have its own folder to put the sitemap.xml in.
  4. Choose the Shop View and Save.
  5. You can now create a sitemap.xml for each Shop View.
  6. When the Cronjob works without errors, you can see this in the Last Generated column.
Configuring Cronjob on a Linux Server using SSH.

We connect to our Linux Webservers using SSH. Login procedure can be configured in a great little application called Putty. More information can  be found on their website: http://www.chiark.greenend.org.uk/~sgtatham/putty/

  1. Connect to the Host using a Hostname or IP address on Port 22 and connection type SSH. In the popup window you are asked for a login name and password. Great now we are on the server.
  2. Type and enter: "crontab -e" to open the crontab. Most likely this will be in an app called VI.
  3. Type "i" to change mode to "insert". Now you are able to edit the crontab.
  4. Now paste the following line:
    */15 * * * * php -f /var/www/vhosts/PATH/TO/MAGE/cron.php > /dev/null 2>&1
    The /dev/ part means that all output (ok and errors) will be output in /dev/null/ folder.
    The 15 * * * * means that this job will be run every 15 minutes.
  5. Following line will reindex the database every day at 4 AM:
    0 5 * * * php -f /var/www/vhosts/PATH/TO/MAGE/shell/indexer.php > /dev/null> 2>&1
  6. Following line will clean the log files every first of the month:
    0 0 1 * * php -f /var/www/vhosts/PATH/TO/MAGE/shell/log.php clean > /dev/null 2>&1
  7. Hit "ESC" to exit "insert" mode.
  8. Enter "Shift+q", type "wq" and hit ENTER to exit the Crontab.
  9. When successful this message appears: "crontab: installing new crontab"
This concludes how to configure a cronjob on a Linux environment and within the Magento Admin. I have used this approach for my Magento based websites http://www.ruitersport-discounter.nl (equestrian budget) and http://www.paardcare.nl (equestrian) with success.