Cron Basics

Cron is a daemon that executes commands at specified intervals. These commands are called "cron jobs." The Cron daemon is available on Unix, Linux and Mac servers. Windows servers use a Scheduled Task to execute commands.

Cron Job Command Basics

The way you set up a CRON job on your server depends on what software you are using to manage your hosting account. Some web hosting control panels (such as cPanel or Plesk) offer a graphical user interface (GUI) which makes it much easier to set up the scheduled task. If you do not have a GUI available, you will have to enter the necessary commands using Telnet or SSH.

A quick search on the Internet for 'cron' followed by the name of your web control panel should yield plenty of articles that tell you how to physically set up a new CRON job, but it is usually fairly self-explanatory if using something like cPanel or Plesk.

In any case, a CRON job is made up of 2 main elements: a command to be run, and an interval definition (i.e. something that tells it how often to run). You might also be asked to provide an e-mail address to which any error messages or other output can be sent.

The command to run is made up of 4 parts:

  • An executable (i.e. a program that is capable of running PHP scripts)

  • One or more configuration options (specific to the executable)

  • An output path (where to put any output from the script)

  • A URL (the script to be called)

In the following example, the crontab command shown below will activate the cron tasks automatically on the hour:

0 * * * * wget -O - -q -t 1 "http://www.yourwebsite.com/index.php?option=com_component&task=cron.xxxx&secret=YourSecretKey"

The 0 * * * * part in the example above is the time when the cron job is going to happen. The following schematic tries to explain the general crontab syntax:

# +---------------- minute (0 - 59)
      # | +------------- hour (0 - 23)
      # | | +---------- day of month (1 - 31)
      # | | | +------- month (1 - 12)
      # | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
      # | | | | |
      * * * * * command to be executed

The frequency with which one wishes a specific script to run, should be carefully evaluated. Running it too often when there is nothing much to do is probably wasteful of system resources. On the other hand not running it frequently enough means that the expected actions are not 'up to date'. Generally once an hour is more than adequate in our tests, especially since we used to manually perform the task twice or three times a day.

Trouble shooting cron

If your cron task is not working, you should:

  • Make sure your website can be triggered via your browser: Click on your cron URL. That will trigger it manually via your browser so you will see if this URL can be reached or not.

  • If you use a third-party SEF component, your website may redirect the current page to the SEF one which will break the own cron service. So you can either configure your SEF component to not transform non-SEF into SEF URLs or directly write your SEF cron URL on the cron interface.

  • Make sure your website can be triggered by the outside: If you have an .htaccess with password protection, your website will obviously not accept to be triggered by an anonymous user.

  • If you use a security component on your Joomla website and use a remote cron facility, this component might block the cron task. If you are using RSFirewall, you could try disabling the DoS protection from the Components => RSFirewall => Configuration => Active scanner configuration options.

  • Sometimes cron jobs can't trigger an URL including a "&" (see above) or can only trigger a script (instead of an URL). In that case, you should create a file with a suitable name at the root of your website including the code below:

    <?php
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,'http://www.yourwebsite.com/index.php?option=com_component&task=cron.xxxx&secret=YourSecretKey');
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_TIMEOUT, 10);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
          curl_setopt($ch, CURLOPT_AUTOREFERER,true);
          echo curl_exec($ch);
          curl_close($ch);

    Then configure your cron to trigger that script.

    Please don't forget to replace 'yourwebsite.com' by the URL of your own website, and also the correct component name, task parameters etc..

  • Make sure that there is no system cache storing up your pages accessed by a front end based cron task. If there is the cron task will only work when the cache entries expire, which will depend upon the cache settings.

  • We have seen circumstances where a cron job that executes fine in PHP with MySQL statements and echo statements, but when mail functions are added it fails with this error:

    sh: -t: command not found

    The CRON daemon can be picky about paths, and in these cases it is worth using absolute physical paths instead of absolute share paths. This is mainly because the CRON daemon is active before the shares are up, and why it is probably unable to process the commands/enumerate the path during the initialisation phase.

    It is also worth while adding the option to direct PHP to the initialisation file to be used, with the '-c' option, to prevent it running from shell context only. This will include the proper path to the sendmail alias and other information such as locales etc.

    * * * * * /usr/local/apache/bin/php -c /php.ini /path_to_cron_php_file/cron_file.php 2> /path_to_log_file/cron_error.txt
Go To Top

Joomla! Debug Console

Session

Profile Information

Memory Usage

Database Queries