Actions

System Administrator's Guide/Cron Job

From Mahara Wiki

< System Administrator's Guide
Revision as of 05:44, 29 September 2012 by Hrynkiw (talk | contribs) (emphasized that running cron via web sends output to web server logs)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Some tasks in Mahara need to be run at scheduled times. This is achieved by use of a cron job. You set up a job to visit a Mahara script every minute, and it takes care of the rest.

Some of the jobs it does include:

  • Updating syndicated RSS feeds
  • Mailing out forum post updates and other batch notifications
  • Cleaning up old files to save space

How to set up the cron job

The cron script is lib/cron.php. You need to set  up a job to visit this script every minute - either through the web by visiting http://your-mahara-site.org/lib/cron.php, or by the command line, calling php /path/to/mahara/htdocs/lib/cron.php. Both ways have their advantages and disadvantages.

Calling cron.php via the web

This is a simple way to get cron going. In your crontab, you need only put a line like this:

* * * * * curl http://your-mahara-site.org/lib/cron.php

It reads, "every minute, use 'curl' to hit the cron.php script".

This way is very simple.

Note: Calling cron.php via the web produces no output to the browser. Output is in the web server error logs, which you don't normally want, but it's better than exposing potentially revealing output to the public.

Calling cron.php via the command line

This way is slightly more difficult, but allows you to log the cron output separately. The trick is, you have to run the cron job as the web server user, so it has all the rights it needs. You can either do this by installing the cron job into the web server user's crontab, or by installing it into root's crontab with the user field set to the web server user:

In the web server user's crontab:

* * * * * php /path/to/mahara/htdocs/lib/cron.php >> /path/to/mahara/cron.log 2>&1

Or, in root's crontab (substitute www-data with the web server user):

* * * * * www-data php /path/to/mahara/htdocs/lib/cron.php >> /path/to/mahara/cron.log 2>&1
  

This way has a couple of caveats:

  • Make sure that the log file can be written to by the web server user. On debian, you could make a directory /var/log/mahara-cron, and chown that to www-data. Then you could make the logfile path /var/log/mahara-cron/cron.log.
  • The php.ini used by command line PHP is not the same as the one used by Apache PHP. E.g., in Debian, Apache uses /etc/php5/apache2/php.ini, while the command line PHP uses /etc/php5/cli/php.ini. So either you make sure both of them are the same, or it may make sense to use the -c flag with your PHP call, like so: * * * * * php -c /etc/php5/apache2/php.ini /path/to/mahara/htdocs/lib/cron.php >> /path/to/mahara/cron.log 2>&1
  • Under some distributions (e.g. Debian), the manner in which extensions are loaded can cause php to Segmentation Fault. This appears to be caused by the php curl module being loaded before the postgres module. You can fix this by moving curl.ini to something lexically later (e.g. Z99_curl.ini).