Sign up to our newsletter to receive notifications of extension updates.

Monday, 07 February 2011 19:49

Creating cron jobs with JCron and the joomla library

Written by 
Creating cron jobs with JCron and the joomla library Victor Nuno

Sometimes it is necessary to run processes on a web site that make changes to the database on a daily basis. An example of this could be when data needs to be aggregated from a reservations component and made available to an external application or tool, in a format that is user friendly. A script would be written that updates an existing table and then the extenal tool could access this table. This script will work fine as a standalone php file but how do we make sure it is run and how do we make use of the Joomla libraries inside it when it is not being called from within the framework?

First of all we will need a script that allows us to use the Joomla class JDatabase.  To do this we need to define a few constants and then include various library files.  Place code like this in your php script.

 

 

<?php
/* Initialize Joomla framework */
define( '_JEXEC', 1 );
define('JPATH_BASE', str_replace('/cron','',dirname(__FILE__)) );
define( 'DS', DIRECTORY_SEPARATOR );
/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* To use Joomla's Database Class */
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
/* Create a database object */
$db =& JFactory::getDBO();

Now Joomla is up and running we can begin to use the JDatabase class the same as if we were writing code within a component, module or plugin.  The example I'm going to use is very simple but it will give you an idea of how to work with the database.

/* Create a table called cron with one column `datetime` */
$query = 'CREATE TABLE IF NOT EXISTS '.$db->nameQuote('#__cron').' '
 .'(`datetime` DATETIME) TYPE=MyISAM';
$db->setQuery($query);
$db->query();
/* Die on error */
if($db->getErrorMsg()) { die; }
$query = 'INSERT INTO '.$db->nameQuote('#__cron').' (`datetime`) VALUES ("2011-01-01 23:59:49")';
$db->setQuery($query);
$db->query();
/* Update the cron table with the current date and time */
$query = 'UPDATE '.$db->nameQuote('#__cron').' SET datetime = "'.date('Y-m-d H:i:s',time()).'"';
$db->setQuery($query);
$db->query();
 

This is a very basic script that will create a table called cron and place the current date and time in that table every time it is run.  Admittedly, this is a pointless exercise but the key thing here is looking at how to get a component called JCron to run this script.  You can create your own complex database queries later ;).

Store the entire script somewhere on your site. I created a folder called cron in the main site directory and placed the script there.

Install JCron and enable cron jobs. In the component parameters set the following options:

Next, assuming your script file is called cron.php, you will want to set up the a cron job as follows:

This setup will run the file cron.php every time someone accesses your site.  It will not run if there are no visits to your site.  This is a useful way to set up cron jobs for a novice and a good workaround if you don't have shell access to your server.

Read 10334 times Last modified on Tuesday, 17 January 2012 22:39

Leave a comment

Make sure you enter the (*) required information where indicated.
Basic HTML code is allowed.