Remitur Posted September 25, 2021 Share Posted September 25, 2021 WHMCS has a hook to run a script daily, together with daily cron job (DailyCronJob: https://developers.whmcs.com/hooks-reference/cron/#dailycronjob ) and two hooks to run a script every five minutes (AfterCronJob and PreCronJob) 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?! 0 Quote Link to comment Share on other sites More sharing options...
Bigol'tastynuggets Posted September 25, 2021 Share Posted September 25, 2021 39 minutes ago, Remitur said: WHMCS has a hook to run a script daily, together with daily cron job (DailyCronJob: https://developers.whmcs.com/hooks-reference/cron/#dailycronjob ) and two hooks to run a script every five minutes (AfterCronJob and PreCronJob) 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 0 Quote Link to comment Share on other sites More sharing options...
Remitur Posted September 25, 2021 Author Share Posted September 25, 2021 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...) 0 Quote Link to comment Share on other sites More sharing options...
Damo Posted September 25, 2021 Share Posted September 25, 2021 Why not set it up (as you've mentioned already) outside of WHMCS? You have full control over its timing then. 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted September 25, 2021 Share Posted September 25, 2021 <?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. 1 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted September 25, 2021 Share Posted September 25, 2021 - https://developers.whmcs.com/hooks-reference/cron/#postautomationtask - https://developers.whmcs.com/hooks-reference/cron/#preautomationtask Runs every five minutes 0 Quote Link to comment Share on other sites More sharing options...
developer _WHM_CS Posted September 26, 2021 Share Posted September 26, 2021 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!'); } } }); 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.