1

I have business logic that interfaces with Drupal entities (in this case, users), and we need to generate reports off these records daily at a preset time. It would be easy for me to generate this data on-demand, but the requirements ask for a daily report.

I'm wondering if its possible in D7 to have the system schedule a specific cron task to run at a preset task over the time cron is expected to run (which right now, is every 3 hours). Another option I am considering is to have a Contrib Drush script that simply runs using the OS'es crontab/launchd. Which one makes the most sense?

2

I had to do something similar recently. The extremely simple answer I came up with was to simply keep track of the last time the function was run with a db variable.

So, in psudo code, I ended up doing something like this:

function hook_cron(){
  $now = new DateTime(date(DATE_ISO8601));
  $last_checked = new DateTime(variable_get('last_checkedvar', date(DATE_ISO8601, '0')));
  $diff = $now->diff( $last_checked );

  if ($diff->days >= 1){
    //do your stuff
  }
}

Works well for me. And it also doesn't interfere with any of the other cron tasks I have running on my site.

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.