Cron

Contents

What is cron?

Cron is a program available on Unix-based operating systems that enables users to run commands and scripts at set intervals or at specific times. A common use of cron is to rotate server logs to prevent them from becoming too large.

Elgg and cron

Elgg has a cron handler for executing functionality periodically. This enables the administrator to setup jobs to be run periodically. Two common examples would be rotating the system log in the database or doing garbage collection in the database (compacting the database by removing entries that are no longer required).

Currently, Elgg supports the following hooks:

  • reboot - Run on server boot
  • minute - Run every minute
  • fiveminute - Run every 5 minutes
  • hourly - Run every hour
  • daily - Run every day
  • weekly - Run every week
  • monthly - Run every month
  • yearly - Run every year

How does it work?

The Elgg cron handler is activated when particular pages are loaded. As an example, loading http://example.com/cron/hourly/ in a web browser would activate the hourly hook. To automate this, a cron job is setup to hit those pages at certain times. This is done by setting up a crontab which is a configuration file that determines when and what cron runs.


The crontab needs to specify a script or command that will hit the Elgg cron pages. Two commonly available programs for this are GET and wget. You will need to determine the location of one of these on your server. Your crontab also needs to specify the location of your website.


Note: If you choose the utility "wget", then we suggest that you specify the option "--output-document" (see manual for wget). For example, under Debian: /usr/bin/wget --output-document=/dev/null. If you don't do that, files will be created in the home directory of the CRON user. The number of created files will increase over and over...

Example crontab

 # Location of GET
 GET='/usr/bin/GET'
 
 # Location of your site (don't forget the trailing slash!)
 ELGG='http://www.example.com/'
 
 # The crontab
 @reboot $GET ${ELGG}cron/reboot/
 @minute $GET ${ELGG}cron/minute/
 @fiveminute $GET ${ELGG}cron/fiveminute/
 @hourly $GET ${ELGG}cron/hourly/
 @daily $GET ${ELGG}cron/daily/
 @weekly $GET ${ELGG}cron/weekly/
 @monthly $GET ${ELGG}cron/monthly/
 @yearly $GET ${ELGG}cron/yearly/

Installation

Take the example and change the ELGG and GET variables according to your server setup. If you have ssh access to your Linux servers, type crontab -e and enter the crontab details into the text editor that results. If you already have a crontab setup, you will have to merge the Elgg information into it. If you don't have ssh access, you will have use a web-based configuration tool to set your crontab. This will vary depending on hosting provider.

On Windows servers, there are a number of cron emulators available.

For wget, you should try using the flag --spider so that it does not download the file each time. For more information on wget, see the wget manual.

Using Cron with Cpanel

For information on using Cron tasks using Cpanel see Cpanel Docs.

In the command field, enter the appropriate link of the cron page. For example, for a weekly cron job, enter the command as http://www.site.com/cron/weekly/ .

The common cron tasks are listed below. Replace www.site.com with your actual website link.

See also

Search docs