Hello,

I'm new to Drupal and I had a question about cron:

I need to run some statistical calculations from database tables every hour and store the results to another table.
I know how to write the drupal function to do this.
But how do I get cron to call my function every hour?

I'm hosted with Bluehost and they allow me to edit my crontab.

So is this what I need to do??

1 - Write my drupal module with the function to do the calculation
2 - Write my php script and in the scrip call my drupal module (e.g. file mycalcs.php)
3 - Put this php script in my root directory (same directory as cron.php)
4 - Configure the bluehost crontab to call mycalcs.php every hour

Will this work?

thank you in advance for your help!!

Comments

dwees’s picture

If you want access to the full range of Drupal functions etc... for your code, then you should create a proper module to do this:

1. Create a file call it 'extra_statistics.module'.
2. Create another file call it 'extra_statistics.info'.
3. Create another file call it 'extra_statistics.install'.

In your first file create a function like so:


// $Id$

/**
 * Implementation of hook_cron().
 */
function extra_statistics_cron() {
  // put your cron code in here
}

In the 2nd file put the following:

; $Id$
name = Extra Statistics
description = "Enables extra site specific statistics calculations from the database."

And in the last file put the following:


// $Id$

/**
 * Implementation of hook_install().
 */
function extra_statistics_install() {
  // read about hook_install functions and enter the proper code here.
}

/**
 * Implementation of hook_uninstall().
 */
function extra_statistics_uninstall() {
  // read about hook_uninstall functions and enter the proper code here.
}

Note that in the above, 2 of the files require an opening <?php tag but no closing ?> tag and the .info file is just a plain text file.

To learn about configuring cron jobs for Drupal, find the Handbook link up top and search through it for 'configuring cron jobs' (or use the search feature), there is a lot of useful information and exact code syntax for cron jobs.

Dave

My site: http://www.unitorganizer.com/myblog

mrbkonline’s picture

Hi Dave,

Wow that's great Drupal has a cron hook, ok I know mostly what to do now!

Only one thing I am not clear on...

Because of the _cron hook, my statistics function will be called every time cron.php is run.

So what if I wanted my statistics function to be called at a different interval than when cron.php is executed?

thank you!

dwees’s picture

Just keep track of the last time you ran the cron job, and don't run your code if its too early. If it isn't too early then run your code and update the database with the time you ran the code. You can probably just use variable_get and variable_set to keep track of this if you don't want to build your own table in the database.

Dave

My site: http://www.unitorganizer.com/myblog

mrbkonline’s picture

yes, that makes sense, I can do that, thank you