What reported by Berdir is correct; implementations of hook_cron()
are invoked during Cron jobs, if you have set the cron task correctly. This means that you need to add a cron job that executes one of the following commands:
/usr/bin/lynx -source http://example.com/cron.php > /dev/null 2>&1
curl --silent --compressed http://example.com/cron.php
As far as I remember, you also need to use the cron key that is reported in Drupal administrative pages.
If, for any reason (I doubt there is any reason for you to do so) you need just to execute that PHP script, and not all the Drupal cron tasks, which means that you have actually set two cron jobs and one is already executing the Drupal cron tasks, then you should add the following lines at the beginning of the PHP file you are trying to execute cron task:
define('DRUPAL_ROOT', 'Set here the directory where Drupal is installed');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
Doing so, the PHP file will have access to all the functions available in bootstrap.inc. If you need a function that is available in another Drupal include file, then you need to include that file with require_once()
; if you need a function that is available from a Drupal module, then you need to add drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL)
after the require_once()
lines.
If you need a function from a module, then you should really implement a custom module (if you don't have already one) that implements hook_cron()
and that includes the PHP file you have to execute it.
As reported by Berdir, a Drupal cron task should not call drupal_set_message()
because when cron tasks are executed, those messages would not be visible. If you need to report an error message that administrator users could see, then the code needs to call watchdog(), which needs Drupal to be full bootstrapped; the function is defined in bootstrap.inc.