CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to set a Daily cron job to run in a specific hour?
Pages: [1]

Author Topic: How to set a Daily cron job to run in a specific hour?  (Read 631 times)

VitorFF

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 1
  • CiviCRM version: 4.4.11
  • CMS version: Drupal 7.27
  • MySQL version: 5.5
  • PHP version: 5.4
How to set a Daily cron job to run in a specific hour?
December 01, 2014, 06:18:51 am
Hello guys,

We have a daily cron job that goes by school's memberships each day and updates the memberships of the related teachers to match it.

I noticed that the cron job doesn't run at the same time each day, this caused an issue where a membership that expired yesterday (30/11/2014) and began again today (01/12/2014) was not updated because the cron job ran at 22PM when the old membership was still valid.

So, I would like this cron job to run each day at the same hour, let's say 00.30AM. How can I set a fixed hour for the cron job to run?

Thank you guys!

VitorFF
Why don't you take a look at this? (You will not regret it!)

Capitalisation Corrector : https://civicrm.org/extensions/capitalisation-corrector

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: How to set a Daily cron job to run in a specific hour?
December 03, 2014, 12:18:54 am
The depends a bit on whether you're asking the question as an administrator (who wants to change the policy on one server) or an extension-developer (who wants to change the default for all deployments).

ADMINISTRATOR: You can turn off the basic scheduler built into Civi and setup more nuanced scheduling directly in crontab, e.g.

1. Navigate to "Administer => System Settings => Scheduled Job" find the job you care about. (It sounds like "Update Membership Statuses" is the one in question.) Note the "API entity" and API action" (e.g. "Job" and "process_membership"). Disable the job.

2. On the webserver, add a new crontab entry like:

Code: [Select]
30 0 * * * cd /var/www/drupal; drush cvapi job.process_membership

EXTENSION DEVELOPER: There's no decisive answer, but there are a few options.

  • Modify your cronjob so that it runs hourly (instead of daily) and then update the logic to check if the current hour is 0. If it's any other hour, then just return without doing any work (e.g. "if ($currentHour != 0) return;"). This is simplest but could be problematic in edge-cases where site-admins want to change the schedule.
  • As above, modify your cronjob to run hourly. Additionally, add a new setting that stores the timestamp of the last run. Write your own logic to read/update the setting and make a decision about whether to proceed. This is a bit more complicated but can handle edge-cases better.
  • Implement hook_civicrm_cron. This hook fires everytime Civi's scheduler runs. One can use this hook to replace the built-in scheduler with a more advanced one (e.g. like the Drupal community did with elysia_cron and ultimate_cron). This would be a bigger task and would merit a new/separate extension.
  • Prepare a patch to the built-scheduler which adds the option of managing the hour.

VitorFF

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 1
  • CiviCRM version: 4.4.11
  • CMS version: Drupal 7.27
  • MySQL version: 5.5
  • PHP version: 5.4
Re: How to set a Daily cron job to run in a specific hour?
December 09, 2014, 01:58:14 am
Hello totten,

thank you so much for your detailed possible solutions, as soon as we decide how to fix the issue I update this post.

Thanks,

VitorFF
Why don't you take a look at this? (You will not regret it!)

Capitalisation Corrector : https://civicrm.org/extensions/capitalisation-corrector

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to set a Daily cron job to run in a specific hour?

This forum was archived on 2017-11-26.