I have a PHP file that does some task in consistent intervals.

I have placed that PHP file inside a custom module folder, but I found that Drupal functions like drupal_set_message() are not available inside the PHP file.

How do I include Drupal functions inside this PHP file? How do I set up this php file to be run as a cron job?


You need to implement hook_cron() in your module and execute these tasks there.

Then, drupal will automatically execute these tasks during the default cron handling.

You probably have used drupal_set_message() only as an example but note that using that function in a hook_cron() implementation makes no sense because there is no user to read these messages. As an alternative, you could log messages with watchdog().

| improve this answer | |

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.

| improve this answer | |
  • 4
    All correct, just two additions. a) Drupal 7 has drupal.org/project/poormanscron built-in, which means that cron tasks are also executed during normal page requests. Doing it with crontab is certainly better however. b) A third option would be to use drush cron, that's what I do nowadays, because it doesn't need cron keys and stuff, just cd drupal/directory; drush cron > /dev/null 2>&1 – Berdir May 5 '11 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.