Sometimes it is desirable to execute actions in the background and periodic intervals. Building on from last week’s post, I wanted to spotlight a new feature, which uses the asynchronous event queue, to allow you to do this – the periodic execution (cron) service.

After completing the configuration step for enabling the Asynchronous Event Queue, you can then run the Known console periodic execution service:

./known.php service-cron

Once running, this service will periodically trigger an event to which code can listen to. Available events are cron/minute, cron/hourly and cron/daily.

Cron, on unix at least, allows commands to be scheduled and executed periodically based on a schedule.

In a web application like Known this can be handy for numerous reasons; scheduling a digest email for users for example, or for scheduling posts to appear at a future time.

Elgg had native cron support, but Known does not (yet). So, I wrote a quick plugin…

Introducing the cron scheduler

The plugin provides a /cron/ endpoint, which is called via code in a crontab script. This script makes a GET call to the endpoint, and the plugin then triggers an event accordingly, e.g. cron/daily for once a day.

The script is protected via a secret code – once you enable and configure the cron plugin, you will be provided with an admin code, which you must add to your script. This provides basic protection from someone triggering the cron tab erroneously.

Anyway, kick it about and let me know what you think!

» Visit the project on Github...

Sometimes things need to be done without user interaction – for example, database optimisation or log rotation.

For this, Elgg has a cron endpoint.

Cron is a unix tool which executes commands at a specific time of day (other operating systems have similar tools). This keys off a file called a crontab – an example is given file is included and called crontab.example.

The crontab calls simplified yet powerful cron endpoint – http://yoursite/pg/cron/PERIOD, where PERIOD is one of the following:

  • reboot – Execute on system reboot
  • minute – Execute every minute
  • fiveminute – Execute every five minutes
  • fifteenmin – Execute every fifteen minutes
  • halfhour – Execute every half hour
  • hourly – Execute once every hour
  • daily – Execute every day
  • weekly – Execute weekly
  • monthly – Execute once a month
  • yearly – Execute every year

When these endpoints are triggered by your crontab a plugin hook is triggered. To make use of this, register a plugin hook as follows:

register_plugin_hook('cron', PERIOD, 'my_cron_handler');

Where PERIOD is one of the key words listed above. Here is some sample code using Cron – in this case it is taken from the system log rotation module I added to SVN today.

* Elgg log rotator.
* @package ElggLogRotate
* @license GNU Public License version 2
* @author Curverider Ltd
* @copyright Curverider Ltd 2008
* @link

* Initialise the plugin.
function logrotate_init()
$period = get_plugin_setting('period','logrotate');
switch ($period)
case 'weekly':
case 'monthly' :
case 'yearly' :
default: $period = 'monthly';

// Register cron hook
register_plugin_hook('cron', $period, 'logrotate_cron');

* Trigger the log rotation.
function logrotate_cron($hook, $entity_type, $returnvalue, $params)
$resulttext = elgg_echo("logrotate:logrotated");
if (!archive_log())
$resulttext = elgg_echo("logrotate:lognotrotated");

return $returnvalue . $resulttext;

// Initialise plugin