Jump to content

How to run a cron only at a given hour?


Remitur

Recommended Posts

WHMCS has

But what if I want a script running at a given hour, different from the hour of the main Daily Cron Job?

The only way (different from creating a different cron job in the system, outside of WHMCS) is:

  • using AfterCronJob or PreCronJob
  • checking if the hour is (about) the right hour to run the job
  • if yes: run
  • if not: return

Simple but... how to check that it's "about" the right hour to run?

If I want to run the script, i.e., at 11:00 , it will be called every five minutes all day long, but there's no warranty about the exact time it will be called each time, so it will be called one or two times in the interval between 10:55:00 and 11:04:59 ...

So, what may be a reliable formula to decide that it's the time to run, skipping the risk to run it twice?!  

 

Link to comment
Share on other sites

39 minutes ago, Remitur said:

WHMCS has

But what if I want a script running at a given hour, different from the hour of the main Daily Cron Job?

The only way (different from creating a different cron job in the system, outside of WHMCS) is:

  • using AfterCronJob or PreCronJob
  • checking if the hour is (about) the right hour to run the job
  • if yes: run
  • if not: return

Simple but... how to check that it's "about" the right hour to run?

If I want to run the script, i.e., at 11:00 , it will be called every five minutes all day long, but there's no warranty about the exact time it will be called each time, so it will be called one or two times in the interval between 10:55:00 and 11:04:59 ...

So, what may be a reliable formula to decide that it's the time to run, skipping the risk to run it twice?!  

 

It runs every 5th minute and is based on the time of your hosting environment I would imagine! 

 

That's only an assumption, as I've never really explored it past that assumption lol 

Link to comment
Share on other sites

9 minutes ago, Bigol'tastynuggets said:

It runs every 5th minute and is based on the time of your hosting environment I would imagine! 

Yes, but for each AfterCronJob you don't know exactly the time that your script will be run: there may be other scripts elsewhere that use that very same hook, and your script may be run before or after them... so "11:00:00" will not be "exactly "11:00:00"... but any time between 11:00:00 and an undefined lapse of time (it depends from the time used by other scripts out of your control...) 

Link to comment
Share on other sites

<?php

use WHMCS\Database\Capsule;

add_hook('AfterCronJob', 1, function($vars) {

    $alreadyRun = Capsule::select(Capsule::raw('SELECT id FROM tblactivitylog WHERE DATE(`date`) = CURDATE() AND description = "I did it!" LIMIT 1'))[0];

    if (date('H') == '14' AND !$alreadyRun) {

        // Do your stuff
        logActivity('I did it!');
    }
});

I'm going back to my cave.

Link to comment
Share on other sites

 check it

 

use WHMCS\Database\Capsule;
use WHMCS\Carbon;

add_hook('AfterCronJob', 1, function($vars) {
if(Carbon::now()->format('H') == '14'){
    $todayRun = Capsule::table('tblactivitylog')->where("description",'like',"I did it!")
	->where("date",">" , Carbon::now()->addMinutes("-30") )->first()->id ;

    if (Carbon::now()->format('H) == '14' AND !$todayRun) {

        // Do your stuff
        logActivity('I did it!');
    }
	}
});

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated