In D8 I programmed hook_cron. I tested it several times but I cannot see any log in the watchlog.

The name of the module is nncbt. The code is placed in nncbt.module.

Any suggestions what I should do?

function nncbt_cron() {  

    $last_run_per_minut = \Drupal::state()->get('nncbt.last_run', 0);
    // If 1 minutes passed since last time.
    if ((REQUEST_TIME - $last_run_per_minut) > 60) {

        \Drupal::logger('nncbt')->info('Runnning Cron every 1 minut log');

        // Update last run.
        \Drupal::state()->set('nncbt.last_run', REQUEST_TIME);


    // Run the following every 5 minutes
    $last_run_per_5_minuts = \Drupal::state()->get('nncbt.last_run_5_minuts', 0);

    // If 5 minutes passed since last time.
    if ((REQUEST_TIME - $last_run_per_5_minuts) > 300) {

        \Drupal::logger('nncbt')->info("Runnning Cron every 5 minut log");

        // Update last run.
        \Drupal::state()->set('nncbt.last_run_5_minuts', REQUEST_TIME);


  • How are you running cron? – Chance G Feb 26 '18 at 21:41
  • Inside, by clicking admin menu> run cron. Also tried the button "Run Cron" on url admin/config/system/cron. – Justme Feb 26 '18 at 21:50
  • Did you rebuild the caches after adding your hook? Is cron completing? Check the status page, cron last run X ago. – Chance G Feb 26 '18 at 21:51
  • If I run the function nncbt_cron() throught developers, execute php code everything is working fine. – Justme Feb 26 '18 at 21:52
  • Then it sounds like drupal doesn't know about your hook. Rebuild the caches. – Chance G Feb 26 '18 at 21:53


The engine will then call the hook whenever a cron run happens, as defined by the administrator.

In other words, hook_cron gets executed after a cron run happens. So if you have cron set to run once a day, then your hook_cron script only gets executed once per day. So you need to set cron to run every 5 minutes or less.

A better alternative, I suggest you use a module like Ultimate Cron which allows you to schedule different cron run times for different cron tasks. This way, cron doesn't run every single task every 5 minutes and instead it runs your specific task only.

  • Even if Drupal's cron runs every 5 minutes there is little additional overhead as tasks are (or should be using state API) scheduled independently of cron running. Having cron run more regularly is desirable; if it only runs once a day then ALL cron tasks are going to run at once, adding to overhead. (assuming you don't have badly written cron tasks that is) – Duncanmoo Nov 26 '18 at 9:16
  • @Duncanmoo which is why I mentioned about using Ultimate Cron in my answer, so you can run specific cron tasks and different times. – No Sssweat Nov 26 '18 at 9:20
  • 1
    Totally, and to confirm that Ultimate cron is the solution to granular control over Drupal's cron (even though its 8 version is in alpha), its interface and options are a bit daunting but it really gives you control. – Duncanmoo Dec 6 '18 at 5:59

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.