I want code to be executed every n minutes. I know there is a way to run cron in Drupal with crontab but in my case it runs every 3 hours.

What would be the best way to achieve this without using crontab? Can I execute code every time a user opens the first page?

2 Answers 2


For Drupal 6 there is Poormanscron module that runs the Drupal cron operation using normal browser/page requests instead of having to set up a crontab to request the cron.php script. If you use Drupal 7 this functionality is included into core. There are external online services that offer online task scheduling such as www.webbasedcron.com You could also try Elysia Cron or Ultimate Cron modules for more control.

  • I'm using Drupal 7. Can you point me to a link with an example please? that would be great. Commented Sep 12, 2011 at 13:55
  • Here are some links I could find. To set up an external cron online service you could read (onlinecronjobs.com/en/faq) For Drupal 7-specific crontab settings (cron.php requires key) please see a short note here (fooninja.net/2010/11/29/drupal-7-and-cron)
    – LLub
    Commented Sep 12, 2011 at 14:45
  • It is preferred to use crontab for frequency < 1 hour but if you have no access to linux shell, the best would be to setup crontab via the command line or your cpanel, DirectAdmin.
    – LLub
    Commented Sep 12, 2011 at 14:46
  • It is worth to see the video at gotdrupal.com/videos/setting-up-drupal-cron especially from 18' where it mentions poormanscron. General info on cron: drupal.org/cron Info about Elysia Cron 2bits.com/drupal-performance/…
    – LLub
    Commented Sep 12, 2011 at 14:46
  • The poormanscron-like solutions take some resources as every time user loads page drupal checks if /if not to run cron.
    – LLub
    Commented Sep 12, 2011 at 14:46

The simplest solution would be to set the cron to run more frequent: every 3 minutes is not unheard of.

However, many modules, including core, do not have a timecheck: they run their code regardless of how long it was since they last ran it. Other modules (also many core) have some "run only every X" implemented.

You will see an impact, but depending on your specific situation, that may, or may not be a problem.

Once that is done, you can implement hook_cron():

function yourmodule_cron() {
  $last_run = variable_get('mymodule_cron_last_run', REQUEST_TIME);

  if ($last_run <= (now() - 180)) {//180 seconds is three minutes. 
    variable_set('mymodule_cron_last_run', REQUEST_TIME); 

If, however, you cannot run cron this often, you may want to write a cron bypass. In Drupal 6, the route would be, to create a menu-item, with a "secret" token in it (to avoid external folks from hitting that URL by accident/to overlad your server). In Drupal 7 there are (not well documented, AFAIK) methods to create thin requests: my code below will bootstrap an entire Drupal, while you may need only a very simple database-query to be ran without accessing any Drupal resources or APIs.

 * Implementation of hook_menu().
function mymodule_menu() {
  $items = array();

  $items['yourmodule/cron/s0mes3cr3tkey'] = array(
    'title'            => 'not used',
    'description'      => 'not used',
    'page callback'    => 'yourmodule_do_epic_stuff',
    'type'             => MENU_CALLBACK,

  return $items;

 * Menu Callback that does epic stuff.
function _mymodule_do_epic_stuff() {
  # Do Epic Stuff.

You can then add an additional crontab to call only that url.

It is a very bad idea to run such code in the request of a user:

  1. You don't know if users are visiting every 3 minutes. Even if you have 1000visitors per minute, there are potential situations where no user visits for, say 10 minutes.
  2. You don't want your users to be bothered with maintainance code you run in their request.
  3. You don't want your users to experience faults or errors in your cron-tasks. These tasks can, e.g. check whether some external API is up or down and throw errors on network trouble and such.
  • Ok, Ill take a look. Sounds interesting! Commented Sep 12, 2011 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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